简体   繁体   中英

why define multiple module with same name in angularjs

I am new at 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.

main.js file

(function () {
    'use strict';

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

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

        }]);
})();

messages.js file

(function () {
    "use strict";

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

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

All of these js files include ngCookService module. 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. Therefore calling angular.module('ngCookService') does not override or replace the module.

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('ngCookService', ['ngRoute']);

If the setter is called more than once, then the module is being overridden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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