[angular]服务之3$injector
2016-12-26 14:03
357 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>测试service的injector</title> <script src="../frameWork/angular.js"></script> <script src="../testService/testServiceInjector.js"></script> </head> <body> <div ng-app="testServiceNormalModule"> <div ng-controller="ctr1" ng-click="show()">fff </div> <div ng-controller="ctr2"></div>ggg </div> </body> </html>
/** * 摘要:由于service在angular中的地位非常重要,所以得认真对待。 * 服务是一个单例对象,与应用同生死,能够注入,是controller之间通讯的主要工具。 * (通信还可以用$emit,$broadcast,$on的形式,但这个方式有局限,两个controller得有关系,以后研究). * 服务依赖provider,这个provider一般是module. * service实例分析第三篇:Injector分析 * Created by liyanq on 16/12/24. */ /**$injector是自带的服务,能和$scope一样使用,可以在controller中使用其它服务的地方使用。 * 1,get:$injector.get(name, [caller]);Return an instance of the service.返回服务的实例 * 2,invoke:$injector.invoke(fn, [self], [locals]);第二个参数是self The `this` for the invoked method. * 3,has:Allows the user to query if the particular service exists. * 4,annotate:Returns an array of service names which the function is requesting for injection. * 5,angular.injector里面的参数是服务名称组成的数组。没有练习,貌似是和外界打交道用,用的时候再说吧。 * */ var moduleNormal = angular.module("testServiceNormalModule",[]); var service1fun = function (window) { this.gameName = "service1.gameName"; }; moduleNormal.service("service1",["$window",service1fun]); moduleNormal.controller("ctr1",["$scope","$injector",function ($scope,$injector) { $injector.invoke(["service1",function (service1) {//里面可以用service1了。 console.log("ctr1中的service1服务"+service1.gameName); console.log($injector.get("service1")); console.log($injector.annotate(service1fun));//["window"] console.log($injector.has("service1"));//true }]); }]).controller("ctr2",["$scope","service1",function ($scope,service1) { console.log("ctr2中的service1服务"+service1.gameName); }]);
相关文章推荐
- Android IPC进程间通讯机制
- android之定时器AlarmManager
- axis备忘
- Windows XP Service Pack 3 RC1 v.3244 winxp补丁3 提供下载
- Run As Service runassrv.exe 详细参数第1/2页
- 安装MySQL在最后的start service停住了解决方法
- Android IPC机制绑定Service实现本地通信
- android使用Messenger绑定Service的多种实现方法
- asp.net Web Service 接口大量数据传输解决方案
- Android Service判断设备联网状态详解
- Silverlight中动态获取Web Service地址
- android调用web service(cxf)实例应用详解
- 在Android中 获取正在运行的Service 实例
- ASP.NET State service状态服务的问题解决方法
- Angular ng-repeat 对象和数组遍历实例
- 卸载ZkeysPHP 后iis网站出现Service Unavailable 解决办法
- IIS Admin Service 服务因 2149647636 (0x80210514) 服务性错误而停止
- angular.element方法汇总
- Service Temporarily Unavailable的503错误是怎么回事?
- 深入学习JavaScript的AngularJS框架中指令的使用方法