AngularJs学习笔记--Injecting Services Into Controllers
原版地址: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>
虽然这样很方便,但是假如我们需要压缩、混淆我们的代码,这可能会导致参数名称被更改,遇到这种情况的时候,我们还是需要使用显式声明依赖的方式。
- AngularJs学习笔记--bootstrap
- AngularJs学习笔记--html compiler
- AngularJs学习笔记--concepts
- AngularJs学习笔记--directive
- AngularJs学习笔记--expression
- AngularJs学习笔记--Forms
- AngularJs学习笔记--I18n/L10n
- AngularJs学习笔记--IE Compatibility
- AngularJs学习笔记--Modules
- AngularJs学习笔记--Scope
- AngularJs学习笔记--Dependency Injection
- AngularJs学习笔记--Understanding the Model Component
- AngularJs学习笔记--Understanding the Controller Component
- AngularJs学习笔记--E2E Testing
- AngularJs学习笔记--Understanding Angular Templates
- AngularJs学习笔记--Using $location
- AngularJs学习笔记--Creating Services
- AngularJs学习笔记--Injecting Services Into Controllers
- AngularJs学习笔记--Managing Service Dependencies
- AngularJs学习笔记--unit-testing
AngularJs学习笔记--Injecting Services Into Controllers的更多相关文章
- AngularJs学习笔记--Creating Services
原版地址:http://docs.angularjs.org/guide/dev_guide.services.creating_services 虽然angular提供许多有用的service,在一 ...
- AngularJs学习笔记--Forms
原版地址:http://code.angularjs.org/1.0.2/docs/guide/forms 控件(input.select.textarea)是用户输入数据的一种方式.Form(表单) ...
- AngularJs学习笔记--expression
原版地址:http://code.angularjs.org/1.0.2/docs/guide/expression 表达式(Expressions)是类Javascript的代码片段,通常放置在绑定 ...
- AngularJs学习笔记--directive
原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...
- AngularJs学习笔记--Guide教程系列文章索引
在很久很久以前,一位前辈向我推荐AngularJs.但当时我没有好好学习,仅仅是讲文档浏览了一次.后来觉醒了……于是下定决心好好理解这系列的文档,并意译出来(英文水平不足……不能说是翻译,有些实在是看 ...
- AngularJs学习笔记--bootstrap
AngularJs学习笔记系列第一篇,希望我可以坚持写下去.本文内容主要来自 http://docs.angularjs.org/guide/ 文档的内容,但也加入些许自己的理解与尝试结果. 一.总括 ...
- AngularJs学习笔记--html compiler
原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...
- AngularJs学习笔记--concepts(概念)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...
- AngularJs学习笔记--Using $location
原版地址:http://code.angularjs.org/1.0.2/docs/guide/dev_guide.services.$location 一.What does it do? $loc ...
随机推荐
- laravel中的验证及利用uploadify上传图片
$rules = [ 'password'=>'required|between:6,20|confirmed', ]; $message = [ 'password.required'=> ...
- oracle数据库中修改已存在数据的字段
在oracle中,如果已经存在的数据的某些列,假如要更换类型的话,有的时候是比较麻烦的, 会出现:ORA-01439: column to be modified must be empty to c ...
- tensorflow使用
近期在工作中使用tensorflow训练神经网络模型,使用方法简要记录. 环境配置
- Backup--压缩备份和TDE
1>对启用TDE的数据库,压缩备份的备份文件大小与未压缩备份的备份文件大小差不多(压缩比为 1 ) 2>对启用TDE的数据库,压缩备份的备份时间远高于未压缩备份 2>对启用TDE的数 ...
- Java反射机制与工厂模式
工厂模式属于创建型模式,它提供了一种创建对象的最佳方式. 它的特点是:客户端的程序类不直接牵扯到对象的实例化管理,只与接口发生关联,通过工厂类获取指定接口的实例化对象. 简单工厂模式如下: inter ...
- “全栈2019”Java第十八章:一元运算符
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- bzoj1800飞行棋
题目链接 简单模拟 真的不敢相信ahoi09年的题竟然是这个难度 首先,一个必须要知道的定理是:圆上只有直径所对的圆周角是直角. 然后就很显然了, 只有两条不重合的直径上的四个点才能组成一个矩形, 所 ...
- 二,php的错误处理
php处理错误的三种方式: 简单的die()语句: 自定义错误和错误触发器:错误日志: 1,简单的die()语句 if(!file_exists("aaa.txt")){ die( ...
- iOS核心动画CALayer和UIView
UIView和CALayer的关系. 每一个UIview都有一个CALayer实例的图层属性,也就是所谓的backing layer. 实际上这些背后关联的图层才是真正用来在屏幕上显示和做动画,UIV ...
- leetcode-819-Most Common Word(词频统计)
题目描述: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...