AngularJs(Part 4)Modules depending on other Modules

Angular does an excellent job of managing object dependencies. it can even take care of module dependencies.
So we can easily group releated services into one module, and make other modules dependent on it.
    angular.module('my',['my1','my2']);
    the preceding code create a module named "my" which dependent on two other modules "my1" and "my2".

So modules can dependent on other modules and services can dependent on other services.
and this raises several interesting questions, which are as follows:
    1. Can a service defined in one AngularJS module depend on services in another module?
    2. Can services defined in a child module depend on a service in a parent module, or only on services defined in child modules?
    3. Can we have module-private services visible only in a certain module?
    4. Can we have several services with the same name defined in different modules?
    
    angular.module('app',['engines'])
    .factory('car',function(dieselEngine){
        return {
            info:function(){
                console.log(dieselEngine.type);
            }
        };
    });

    angular.module('engines',[])
    .factory('dieselEngine',function(){
            return {
                type:'a sport car'
            };
    });
    above code can execute with no problems.
    what's more surprising is that services defined on sibling modules are also visible to each other. check below code snippet out:
    
    angular.module('my',['car','engines']);
    
    angular.module('car',[])
    .factory('carlog',function(dieselEngine){
            return {
                info:function(){
                    console.log(dieselEngine.type);
                }
            };
    });
    
    angular.module('engines',[])
    .factory('dieselEngine',function(){
            return {
                type:'a sport car'
            };
    });

conclusion: a service defined in one of the application's modules is visible to all the other modules. In other words,
        hierarchy of modules doesn't influence services' visibility to other modules. when Angujar bootstraps an application,
        it combines all the services defined across all the modules into one application, that is , global namespace.
        
    Since Angular combines all the services from all modules into one big namespace.
    there can be only one service with a given name.
    
    currently, there is no way to restrict service's visibility to the other modules.
    
    
    
    

原文地址:https://www.cnblogs.com/formyjava/p/4166313.html