Javascript AngularJS 多次定义 angular.module()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14371410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS defining angular.module() multiple times
提问by Marty Pitt
What is the behaviour of calling angular.module('myModule')multiple times?
angular.module('myModule')多次调用是什么行为?
For example, I wish to define my routes and my directives in separate .js files.
例如,我希望在单独的 .js 文件中定义我的路由和指令。
Is this safe?
这安全吗?
eg:
例如:
//routes.js
angular.module('app',['$strap'])
.config(function($routeProvider, $locationProvider) {
...
});
//directives.js
angular.module('app')
.directive('formInput', function() {
...
Also, what is the impact of defining the dependencies multiple times? Is this additive, or last-in-wins?
另外,多次定义依赖关系有什么影响?这是附加的还是最后的胜利?
eg:
例如:
回答by Mark Rajcok
angular.module(name[, requires], configFn);
...
requires(optional) – {Array.=} – If specified then new module is being created. If unspecified then the the module is being retrieved for further configuration. -- angular.module docs
angular.module(name[, requires], configFn);
...
requires(optional) – {Array.=} – 如果指定,则正在创建新模块。如果未指定,则正在检索模块以进行进一步配置。-- angular.module 文档
I would interpret that as follows: you can only define the dependencies once -- the first time you call angular.module for a particular module. You can call angular.module() multiple times after that, but the requiresoption must not be specified.
我将其解释如下:您只能定义一次依赖项——第一次为特定模块调用 angular.module 。之后您可以多次调用 angular.module() ,但requires不得指定该选项。
回答by James Lawruk
You should only createyour module once. According to the docs, if you create a module with a name that already exists, it will overwrite the previous one. (So last-in-wins.)
您应该只创建一次模块。根据docs,如果您创建一个名称已经存在的模块,它将覆盖前一个。(所以最后的胜利。)
angular.module('app', []);
You can retrieveyour module as many times as you like and in separate files if you wish. You will typically retrieve your module multiple times to declare services, controllers, directives, etc.
你可以检索你的模块很多次,你喜欢和在单独的文件,如果你想。您通常会多次检索您的模块以声明服务、控制器、指令等。
angular.module('app').service('myService', ...);
angular.module('app').controller('myController', ...);
angular.module('app').directive('myDirective', ...);
In the AngularJS docs on Modules, see the section called Creation versus Retrieval.
在AngularJS 的 Modules 文档中,请参阅名为Creation 与 Retrieval的部分。
回答by dsldsl
I'm new to angular, but this is my understanding: you create one module in each file with a namespaced module name, and in your main module you require those modules.
我是 angular 的新手,但这是我的理解:您在每个文件中创建一个具有命名空间模块名称的模块,并且在您的主模块中您需要这些模块。
// in main app.js file
var app = angular.module('myapp',
['myapp.routers', 'myapp.directives', 'myapp.filters']);
// in filters.js
angular.module('myapp.filters', []).filter(....)
// in routers.js
angular.module('myapp.routers', []).router(....)
// in directives.js
angular.module('myapp.directives', []).directive(....)

