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

  angular允许service将其他service声明为依赖,使用在自身实例化时使用的构造函数中。

  为了声明依赖,我们需要在工厂方法声明中指定它们,并且在工厂方法中通过$inject属性(字符串标识数组)或者使用array notation。

  通常$inject属性声明可以被丢弃(即http://www.cnblogs.com/lcllao/archive/2012/10/16/2726967.html中提到的隐式依赖注入,但这个是实验属性,在而且在压缩混淆后会失效,慎用!)。

  • 使用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。

 
    /**
* 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

AngularJs学习笔记--Managing Service Dependencies的更多相关文章

  1. AngularJs学习笔记--Forms

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/forms 控件(input.select.textarea)是用户输入数据的一种方式.Form(表单) ...

  2. AngularJs学习笔记--expression

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/expression 表达式(Expressions)是类Javascript的代码片段,通常放置在绑定 ...

  3. AngularJs学习笔记--directive

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...

  4. AngularJs学习笔记--Guide教程系列文章索引

    在很久很久以前,一位前辈向我推荐AngularJs.但当时我没有好好学习,仅仅是讲文档浏览了一次.后来觉醒了……于是下定决心好好理解这系列的文档,并意译出来(英文水平不足……不能说是翻译,有些实在是看 ...

  5. AngularJs学习笔记--bootstrap

    AngularJs学习笔记系列第一篇,希望我可以坚持写下去.本文内容主要来自 http://docs.angularjs.org/guide/ 文档的内容,但也加入些许自己的理解与尝试结果. 一.总括 ...

  6. AngularJs学习笔记--html compiler

    原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...

  7. AngularJs学习笔记--concepts(概念)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...

  8. AngularJs学习笔记--Using $location

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/dev_guide.services.$location 一.What does it do? $loc ...

  9. AngularJs学习笔记--unit-testing

    原版地址:http://docs.angularjs.org/guide/dev_guide.unit-testing javascript是一门动态类型语言,这给她带来了很强的表现能力,但同时也使编 ...

随机推荐

  1. innerText兼容性问题

    /* text方法,给网页元素设置文本值的方法 主要处理火狐不支持innerText这个属性的问题. 还学习了如何判断一个字符串类型的属性是否存在 如果判断一个对象类型的属性是否存在,用if(ele. ...

  2. 一篇文章让你快速入门 学懂Shell脚本

    Shell脚本,就是利用Shell的命令解释的功能,对一个纯文本的文件进行解析,然后执行这些功能,也可以说Shell脚本就是一系列命令的集合. Shell可以直接使用在win/Unix/Linux上面 ...

  3. VC:GetWindowRect、GetClientRect、ScreenToClient与ClientToScreen

    GetWindowRect是取得窗口在屏幕坐标系下的RECT坐标(包括客户区和非客户区),这样可以得到窗口的大小和相对屏幕左上角(0,0)的位置. GetClientRect取得窗口客户区(不包括非客 ...

  4. U盘安装Centos6.2

    原文地址:http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0819/8452.html. 第一步:制作系统U盘(略). 第二步:设置BI ...

  5. xe7 android如何打包SQLITE数据库

    点击 project->deployment 增加你的SQLite 文件  即可.记住 remotepath 选择assets\internal

  6. C#中使用Redis学习二 在.NET4.5中使用redis hash操作

    上一篇>> 摘要 上一篇讲述了安装redis客户端和服务器端,也大体地介绍了一下redis.本篇着重讲解.NET4.0 和 .NET4.5中如何使用redis和C# redis操作哈希表. ...

  7. [Erlang00]:gen_server:reply/2

    --- gen_server:reply/2 reply(Client, Reply) –> Result      Types:     Client - see below     Repl ...

  8. xshell无法连接Ubuntu的解决办法

    使用workstations14安装完Ubunu后,网络连接方式为NAT模式(N):用于共享主机的IP地址 此时想用xshell连接此虚拟机但是提示连接失败,但是宿主机和虚拟机互相都能ping通,且虚 ...

  9. httpclient 解析excel

    http://www.blogjava.net/jayslong/archive/2011/04/21/convert_xls_and_xlsx_to_csv.html 分享用Java将Excel的x ...

  10. openedx使用中可能用到的一些资源

    这几天一直在弄openedx,你会发现安装好只是第一步,后面还有很多东西在等着你,那么哪里可以看到较新的资料了,分享几个站点: 1.https://readthedocs.org/projects/e ...