AngularJS模块依赖注入与父模块共享它的依赖

AngularJS module dependency injection sharing its dependencies with parent module

本文关键字:模块 依赖 共享 注入 AngularJS      更新时间:2023-09-26

我有一个AngularJS模块:

angular.module("app", ['ui.bootstrap', 'ngRoute', 'ngGridPanel']).config(function($routeProvider) {

你可以看到ngGridPanel是通过依赖注入包含的。

下面是ngGridPanel的模块/指令的定义:

angular.module('ngGridPanel', ['ngAnimate']).directive('gridPanel', ['$animate', '$compile', '$window', '$document', '$timeout', function ($animate, $compile, $window, $document, $timeout) {

如您所见,它指的是ngAnimate

我遇到的问题是,一旦我注入ngGridPanel到我的应用程序中,我的应用程序中的每个元素都突然试图动画。

现在,正如在这个Angular.js GitHub问题中所描述的,ngAnimate将假设所有东西都应该是动画的。一旦我意识到这是预期的行为,我意识到我从来没有包括ngAnimate到我的应用模块在第一个地方

那么为什么它会影响我的整个应用程序呢?它不应该只在属于ngGridPanel模块的指令中生效吗?

那么ngAnimate是如何影响父模块作用域的?这正常吗?


旁注:在这一点上,我甚至没有使用ngGridPanel指令。我只是把它注入了我的应用。

旁注2:一旦我在我的应用程序中实现了一个类名过滤器($animateProvider.classNameFilter(/enable-animate/);),动画停止在我所有的元素上,但仍然在ngGridPanel指令中工作,而不必在任何地方添加enable-animate

如果你依赖于ngGridPanel和ngGridPanel依赖于ngAnimate,那么你也依赖于ngAnimate。

这对你来说是完全一样的,如果你已经定义了你的应用程序angular.module("app", ['ui.bootstrap', 'ngRoute', 'ngGridPanel', 'ngAnimate']) .

至于你的旁注2,似乎很可能他们已经配置它使用类似.classNameFilter()的东西,以便动画不会中断,如果他们的库的用户决定配置它不同,如你所做的。我对ngAnimate了解不多所以这只是我的直觉