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 ...
随机推荐
- 关于搜索elasticsearch的数据条数大于10000的坑 max_result_window的两种设置方式
当用elasticsearch进行深度分页查询时的size-from大于10000的时候,就会报错“”, 官方推荐是scroll查询返回结果是无序的不满足业务需求,所以还是通过设置最大返回结果数来达到 ...
- gulp-usemin 插件使用
关于什么是gulp,它和grunt有什么区别等问题,这里不做任何介绍.本文主要介绍如何使用gulp-usemin这款插件,同时也会简单介绍本文中用到的一些插件. 什么是gulp-usemin 用来将H ...
- Java 实现 WC.exe
Github:https://github.com/YJOED/Code/tree/master/WC/src 一.题目:实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他 ...
- 新入门PGSQL数据库(尝试利用PGPOOL实现分布式),摘录笔记
概念: PostgreSQL (pronounced "post-gress-Q-L") is an open source relational database managem ...
- C++裁剪文件,截断文件,_chsize()
errno_t _chsize_s( int fd, __int64 size ); 详见msdn知识库 _chsize将文件裁剪为指定大小,大小的度量方法与 long ftell(FILE * fp ...
- Win7 家庭普通版开启超级管理员
将以下内容copy保存为admin.reg文件 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ ...
- C#反射(转载)
转载原文出处忘了,一直保存在本地(勿怪) 前期准备 在VS2012中新建一个控制台应用程序(我的命名是ReflectionStudy),这个项目是基于.net 4.0.接着我们打开Program.cs ...
- [NOI2010]能量采集(莫比乌斯反演)
题面: bzoj luogu NOI2010能量采集 题解 读完题之后我们发现在每个产生贡献的点\((x1,y1)\)中,它与原点之间的点\((x2,y2)\)都满足\(x2|x1\),\(y2|y1 ...
- Windows系统下安装 CMake
在安装caffe框架的时候需要用到cmake,特将cmake的安装总结如下: 1 什么是cmake CMake是一个跨平台的编译(Build)工具,可以用简单的语句来描述所有平台的编译过程.CMake ...
- “全栈2019”Java第六十章:如何定义接口
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...