2015-12-04 2 views

ответ

1

Это одно из ограничений системы модулей Angular. Порядок работы при разрешающем зависимостей эффективно:

  1. Первичного модуль
  2. Последний зависимый модуль регистрации

Представьте себе следующую установку:

angular.module('alpha', []) 
    .service('fooService', fooService); 

angular.module('beta', []) 
    .service('fooService', someOtherFooService); 

angular.module('my-app', ['alpha', 'beta']); 

В этом случае , someOtherFooService всегда будет зависимой инъекцией whe n запрашивая его в вашем приложении.

Теперь, если мы изменим приведенный выше пример этому:

angular.module('alpha', []) 
    .service('fooService', fooService); 

angular.module('my-app', ['alpha']) 
    .service('fooService', myFooService); 

Тогда myFooService всегда будет один впрыскивается, поскольку он является основным модулем.

Вы можете увидеть более подробный пример того, как это происходит в примере ниже.

(function() { 
 

 
    function serviceFactory(msg) { 
 
    return function() { 
 
     this.sayHello = function() { 
 
     return msg; 
 
     }; 
 
    } 
 
    } 
 

 
    function rollupService(alphaService, betaService, gammaService, deltaService) { 
 

 
    this.rollup = function() { 
 
     return [ 
 
     alphaService.sayHello(), 
 
     betaService.sayHello(), 
 
     gammaService.sayHello(), 
 
     deltaService.sayHello(), 
 
     ].join(" | "); 
 
    }; 
 
    } 
 

 
    angular.module('alpha', []) 
 
    .service('alphaService', serviceFactory("alpha module")) 
 
    .service('betaService', serviceFactory("alpha module")) 
 
    .service('gammaService', serviceFactory("alpha module")) 
 
    .service('deltaService', serviceFactory("alpha module")) 
 
    .service('rollupService', rollupService) 
 
    .run(function(rollupService, $rootScope){ 
 
     $rootScope.rollup = rollupService.rollup(); 
 
    }); 
 

 
    angular.module('beta', []) 
 
    .service('betaService', serviceFactory("beta module")) 
 
    .service('gammaService', serviceFactory("beta module")) 
 
    .service('deltaService', serviceFactory("beta module")) 
 

 
    angular.module('gamma', []) 
 
    .service('gammaService', serviceFactory("gamma module")) 
 
    .service('deltaService', serviceFactory("gamma module")) 
 

 
    angular.module('my-app', ['alpha', 'beta', 'gamma']) 
 
    .service('deltaService', serviceFactory("my-app module")) 
 
    .controller('myCtrl', function(alphaService, betaService, gammaService, deltaService) { 
 
     this.alphaService = alphaService; 
 
     this.betaService = betaService; 
 
     this.gammaService = gammaService; 
 
     this.deltaService = deltaService; 
 
    }); 
 

 
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<div ng-app="my-app" ng-controller="myCtrl as ctrl"> 
 
    <h3><code>alphaService.sayHello() = </code>{{ctrl.alphaService.sayHello()}}</h3> 
 
    <h3><code>betaService.sayHello() = </code>{{ctrl.betaService.sayHello()}}</h3> 
 
    <h3><code>gammaService.sayHello() = </code>{{ctrl.gammaService.sayHello()}}</h3> 
 
    <h3><code>deltaService.sayHello() = </code>{{ctrl.deltaService.sayHello()}}</h3> 
 
    <hr /> 
 
    <p>Alpha modules dependencies are controller by the application, not how it defined 
 
    it's own dependencies. So watch out cause this could break things.</p> 
 
    <p>This code is run from alpha module's <code>.run</code> block.</p> 
 
    <h3>{{rollup}}</h3> 
 
</div>

Смежные вопросы