AngularJs學習筆記--Managing Service Dependencies

原版地址:http://docs.angularjs.org/guide/dev_guide.services.managing_dependencieshtml

  angular容許service將其餘service聲明爲依賴,使用在自身實例化時使用的構造函數中。angularjs

  爲了聲明依賴,咱們須要在工廠方法聲明中指定它們,而且在工廠方法中經過$inject屬性(字符串標識數組)或者使用array notation。express

  一般$inject屬性聲明能夠被丟棄(即http://www.cnblogs.com/lcllao/archive/2012/10/16/2726967.html中提到的隱式依賴注入,但這個是實驗屬性,在並且在壓縮混淆後會失效,慎用!)。bootstrap

  • 使用array notation
function myModuleCfgFn ($provide) {
       $provide.factory(‘myService’,[‘dep1’,’dep2’,function(dep1,dep2){}]);
}
  • 使用$inject屬性
    function myModuleCfgFn($provide) {
           var myServiceFactory = function(dep1, dep2) {};
           myServiceFactory.$inject = ['dep1', 'dep2'];
           $provide.factory('myService', myServiceFactory);
    }
  • 使用隱式DI(不兼容壓縮混淆的代碼)
function myModuleCfgFn($provide) {
  $provide.factory('myService', function(dep1, dep2) {});
}

       下面有一個例子,裏面有兩個service,它們之間存在依賴關係,以及其餘一些angular提供的service。api

 

 
    /**
    * batchLog service 容許消息在內存中造成隊列,50秒flush一次。
    *
    * @param {*} message Message to be logged.
    */
    function batchLogModule($provide){
      $provide.factory('batchLog', ['$timeout', '$log', function($timeout, $log) {
        var messageQueue = [];
        function log() {
          if (messageQueue.length) {
            $log('batchLog messages: ', messageQueue);
            messageQueue = [];
          }
          $timeout(log, 50000);
        }
        log(); 
        return function(message) {
          messageQueue.push(message);
        }
      }]);
      /**
      * routeTemplateMonitor監控每個route的變化,每一個比阿奴啊都會經過batchLog service記錄下來
      */
      $provide.factory('routeTemplateMonitor',
        ['$route', 'batchLog', '$rootScope',
        function($route, batchLog, $rootScope) {
          $rootScope.$on('$routeChangeSuccess', function() {
            batchLog($route.current ? $route.current.template : null);
          });
      }]);
    }
    // 得到主service,運行應用(監聽事件)
  angular.injector([batchLogModule]).get('routeTemplateMonitor');
 

例子中須要注意的事項:數組

  • batchLog service依賴angular內置的$timeout(http://docs.angularjs.org/api/ng.$timeout)與$log services(http://docs.angularjs.org/api/ng.$log),實現經過console.log批量log消息。
  • routeTemplateMonitor service依賴內置的$route(http://docs.angularjs.org/api/ng.$route) service與咱們自定義的batchLog service。
  • 咱們兩個service都使用工廠方法簽名以及array notation來註釋inject,聲明它們的依賴。array中的字符串標識的順序與工廠方法簽名(參數)中的順序必須一致,這十分重要。除非在工廠方法參數中使用隱式依賴聲明,不然,injector將根據array中字符串的順序決定inject哪個服務。

 

 

 

 

 

 

 

 

 

  1. AngularJs學習筆記--bootstrap
  2. AngularJs學習筆記--html compiler
  3. AngularJs學習筆記--concepts
  4. AngularJs學習筆記--directive
  5. AngularJs學習筆記--expression
  6. AngularJs學習筆記--Forms
  7. AngularJs學習筆記--I18n/L10n
  8. AngularJs學習筆記--IE Compatibility
  9. AngularJs學習筆記--Modules
  10. AngularJs學習筆記--Scope
  11. AngularJs學習筆記--Dependency Injection
  12. AngularJs學習筆記--Understanding the Model Component
  13. AngularJs學習筆記--Understanding the Controller Component
  14. AngularJs學習筆記--E2E Testing
  15. AngularJs學習筆記--Understanding Angular Templates
  16. AngularJs學習筆記--Using $location
  17. AngularJs學習筆記--Creating Services
  18. AngularJs學習筆記--Injecting Services Into Controllers
  19. AngularJs學習筆記--Managing Service Dependencies
  20. AngularJs學習筆記--unit-testing
相關文章
相關標籤/搜索