目前,我们为angularjs应用设置了一个设置,其中有一个名为“ app.js”的文件,其中包含

var app = angular.module('VolumeOutputOptions', [
    'someDirectiveModule',
    'someServiceModule'
]);


除了在诸如someDirectiveModule之类的应用之间共享的代码外,几乎每个类都依赖于此全局变量。有时会导致添加新依赖项的问题,但是事实证明,在没有包含该依赖项的上下文中使用了“ app”。此外,全局变量也很糟糕。

有没有更好的方法可以在您应用的所有指令和控制器之间共享单个模块?例如,将

angular.module('VolumeOutputOptions').directive(...);

angular.module('VolumeOutputOptions').controller(...);


是两个具有相同名称的独立模块,还是angular将它们检测并合并?

最佳答案

如果您使用像

angular.module('VolumeOutputOptions').directive(...);

angular.module('VolumeOutputOptions').controller(...);


要使用该模块,必须先使用angular.module('VolumeOutputOptions', [])这样的模块才能创建模块。
然后将它们都分配给相同的模块VolumeOutputOptions

如果使用第二个数组参数定义像angular.module('moduleName', [])这样的模块作为依赖项,则仅angular创建一个新模块。如果没有传递数组参数,则angular将引用之前声明的具有相同名称的模块。

关于javascript - AngularJs中的共享模块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32757809/

10-12 14:11
查看更多