简体   繁体   English

为什么在angularjs中定义多个具有相同名称的模块

[英]why define multiple module with same name in angularjs

I am new at angularjs. 我是angularjs的新手。 I am looking an application code. 我正在寻找应用程序代码。

<html> 
 .....
<script src="/my/app.main.js"></script>
<script src="/my/app.messages.js"></script>
<script src="/my/app.misc.js"></script>
<script src="/my/app.properties.js"></script>

and in these javascript filse icluding angularjs modules same name. 并且在这些javascript filse中(包括angularjs模块)具有相同的名称。

main.js file main.js文件

(function () {
    'use strict';

    var app = angular.module('ngCookService');

    app.run([.....
        function (....) {            

        }]);
})();

messages.js file messages.js文件

(function () {
    "use strict";

    var app = angular.module('ngCookService');

    app.config(['$stateProvider', function ($stateProvider) { 
       ................
    }]);
})();

All of these js files include ngCookService module. 所有这些js文件都包含ngCookService模块。 And modules defined in self-invoking functions. 以及在自调用功能中定义的模块。

How this system works? 该系统如何工作? Does earlier defined modules overriding? 先前定义的模块是否覆盖? or new defined app module kills earlier objects? 还是新定义的应用模块杀死了较早的对象?

When called with just the name of the module, angular.module() is a getter method and retrieves an existing module with that name. 仅使用模块名称调用时, angular.module()是一种getter方法,并使用该名称检索现有模块。 Therefore calling angular.module('ngCookService') does not override or replace the module. 因此,调用angular.module('ngCookService')不会覆盖或替换模块。

For the getter to work, somewhere before it the setter must have been called. 为了使吸气剂正常工作,必须在它之前的某个位置调用它。 This is done by calling angular.module() with more than 1 argument (for example, a list of required modules)... 这是通过调用带有多个参数(例如,所需模块列表angular.module()来完成的。

angular.module('ngCookService', ['ngRoute']);

If the setter is called more than once, then the module is being overridden. 如果设置程序被多次调用,则该模块被覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM