首页 技术 正文
技术 2022年11月10日
0 收藏 599 点赞 4,075 浏览 3191 个字

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

  把service当作被依赖的资源加载到controller中的方法,与加载到其他服务中的方法很相似。

  由于javascript是一个动态语言,DI不能弄明白应该通过static types(like in static typed languages)注入哪一个service。因此,我们需要通过$inject属性指定service名称, 它是一个包含需要注入的service名称的字符串数组。service ID顺序的重要性:工厂方法中的参数顺序,与service在数组中的顺序一致。工厂方法的参数名称并不重要,但是按照惯常的做法,他们与service ID一一匹配,下面将讨论这样做的好处。

1.显式依赖注入

 

function myController($scope,$loc,$log) {
  $scope.firstMethod = function() {
  //使用$location service
 $loc.setHash();
};
  $scope.secondMethod = function() {
//使用$log service
$log.info(‘…’)
  };
}
myController.$inject = [‘$location’,’$log’];

 

例子:

 

<!DOCTYPE HTML>
<html lang="zh-cn" ng-app="MainApp">
<head>
<meta charset="UTF-8">
<title>explicit-inject-service</title>
</head>
<body>
<div ng-controller="MyController">
<input type="text" ng-model="msg"/>
<button ng-click="saveMsg()">save msg</button>
<ul>
<li ng-repeat="msg in msgs">{{msg}}</li>
</ul>
</div>
<script src="../angular-1.0.1.js" type="text/javascript"></script>
<script type="text/javascript">
var app = angular.module("MainApp",[],function($provide) {
$provide.factory("notify",["$window","$timeout",function(win,timeout) {
//这里是服务依赖服务,通过这种显式的方式,参数名可以乱填,但顺序要对应
var msgs = [];
return function(msg) {
msgs.push(msg);
if(msgs.length==3) {
timeout(function() {
win.alert(msgs.join("\n"));
msgs = [];
},10);
}
}
}]);
}); function MyController($s,$noti) {
//这里是controller依赖服务,通过这种显式的方式,参数名可以乱填,但顺序要对应
$s.msgs = [];
$s.saveMsg = function() {
this.msgs.push(this.msg);
$noti(this.msg);
this.msg = "";
};
}
//指定注入的东东
//也可以参考http://www.cnblogs.com/lcllao/archive/2012/10/16/2725317.html里面的例子
MyController.$inject = ['$scope','notify'];</script>
</body>
</html>

 

2. 隐式依赖注入

  angular DI的一个新特性,允许通过参数名称决定依赖。让我们重写上面的例子,展示如何隐式注入$window、$scope与notify service。

例子:

 

<!DOCTYPE HTML>
<html lang="zh-cn" ng-app="MainApp">
<head>
<meta charset="UTF-8">
<title>implicit-inject-service</title>
</head>
<body>
<div ng-controller="MyController">
<input type="text" ng-model="msg"/>
<button ng-click="saveMsg()">save msg</button>
<ul>
<li ng-repeat="msg in msgs">{{msg}}</li>
</ul>
</div>
<script src="../angular-1.0.1.js" type="text/javascript"></script>
<script type="text/javascript">
var app = angular.module("MainApp",[],function($provide) {
$provide.factory("notify",function($window,$timeout) {
//服务依赖服务,隐式依赖,名称一致即可
var msgs = [];
return function(msg) {
msgs.push(msg);
if(msgs.length==3) {
$timeout(function() {
$window.alert(msgs.join("\n"));
msgs = [];
},10);
}
}
});
}); function MyController($scope,notify) {
//服务依赖服务,隐式依赖,名称一致即可
$scope.msgs = [];
$scope.saveMsg = function() {
this.msgs.push(this.msg);
notify(this.msg);
this.msg = "";
};
}
</script>
</body>
</html>

 

  虽然这样很方便,但是假如我们需要压缩、混淆我们的代码,这可能会导致参数名称被更改,遇到这种情况的时候,我们还是需要使用显式声明依赖的方式。

  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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,488
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289