[英]Sharing Modules in AngularJs
We currently have a setup for our angularjs apps, where there's a file called "app.js" which holds 目前,我们为angularjs应用设置了一个设置,其中有一个名为“ app.js”的文件,其中包含
var app = angular.module('VolumeOutputOptions', [
'someDirectiveModule',
'someServiceModule'
]);
Apart from code shared between apps like someDirectiveModule, almost every class depends on this global variable. 除了在诸如someDirectiveModule之类的应用之间共享的代码外,几乎每个类都依赖于此全局变量。 This has occasionally led to issues where a new dependency is added, but it turns out that "app" is used in contexts where that dependency hasn't been included.
有时会导致添加新依赖项的问题,但是事实证明,在没有包含该依赖项的上下文中使用了“ app”。 Also, global variables, bleugh.
此外,全局变量也很糟糕。
Is there a better way to share a single module between all the directives and controllers of your app? 有没有更好的方法可以在您应用的所有指令和控制器之间共享单个模块? For example, would
例如,
angular.module('VolumeOutputOptions').directive(...);
angular.module('VolumeOutputOptions').controller(...);
be two separate modules with the same name, or would angular detect and merge them? 是两个具有相同名称的独立模块,还是angular将它们检测并合并?
If you use like 如果你用像
angular.module('VolumeOutputOptions').directive(...);
angular.module('VolumeOutputOptions').controller(...);
to use like this there must be a something like angular.module('VolumeOutputOptions', [])
to create the module before it use. 要使用这样的模块,必须先使用
angular.module('VolumeOutputOptions', [])
来创建模块,然后再使用它。 then both of them are assign to same module VolumeOutputOptions
. 然后将它们都分配给相同的模块
VolumeOutputOptions
。
if you define a module like angular.module('moduleName', [])
with the second array parameter for dependency then only angular create a new module. 如果您使用第二个依赖数组参数定义诸如
angular.module('moduleName', [])
类的模块angular.module('moduleName', [])
则仅angular创建一个新模块。 If there is no array parameter passed then angular will refer to the previous module which declared with same name. 如果没有传递数组参数,则angular将引用之前声明的具有相同名称的模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.