<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="UTF-8">
<script src="js/angular.js"></script>
<title></title>
</head>
<body>
<div ng-controller="myCtrl"> </div>
<script>
var app = angular.module('myApp', ['ng']);
//通过service方法创建自定义服务
app.service('$test', function () {
this.info = 'it is a test';
}) //得到注入器 个人理解为在此处继承了$text的服务
var injector = angular.injector(['ng', 'myApp']);
console.dir(injector);
//手工判断该服务是否存在
var result = injector.has('$test')
console.log(result);
//如果存在,得到该服务对象,调用属性或者方法
if (result) {
var testObj = injector.get('$test');
console.log(testObj.info);
} //采用行内式依赖注入
app.controller('myCtrl',
['$scope','$injector' ,
function ($scope,$injector ) {
if($injector.has('$test'))
{
var result = $injector.get('$test').info;
// 或者var result = testObj.info;
console.log("in myCtrl is "+ result)
}
}])
</script>
</body>
</html>

invoke()

使用注入器的invoke()方法,可以直接调用一个用户自定义的函数体,并通过函数参数 注入所依赖的服务对象,这是AngularJS推荐和惯例的用法:

angular.injector(['ng'])

.invoke(function($http){

//do sth. with $http

});

get()

也可以使用注入器的get()方法,获得指定名称的服务实例:

varmy$http=angular.injector(['ng']).get('$http');

//do sth. with my$http

												

Angular injector注入器的更多相关文章

  1. AngularJs angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

  2. Angular - - angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

  3. angular.injector()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. ng 依赖注入

    将依赖的对象注入到当前对象,直接去使用依赖的对象即可. 降低耦合度.提高开发速度.. 文件压缩:yui-compressor有两种方案:①CLI(command line interface)java ...

  5. (七)理解angular中的module和injector,即依赖注入

    (七)理解angular中的module和injector,即依赖注入 时间:2014-10-10 01:16:54      阅读:63060      评论:1      收藏:0      [点 ...

  6. 理解angular中的module和injector,即依赖注入

    理解angular中的module和injector,即依赖注入 依赖注入(DI)的好处不再赘言,使用过spring框架的都知道.angularjs作为前台js框架,也提供了对DI的支持,这是java ...

  7. 探究Angular依赖注入对象$injector

    $injector其实是一个IOC容器,包含了很多我们通过.module()和$provide创建的模块和服务.$injector服务提供了对依赖注入器对象的访问,当然我们也可以调用angular.i ...

  8. 注入器(injector)

    1.0    注入器/injector 注入器是AngularJS框架实现和应用开发的关键,这是一个DI/IoC容器的实现. AngularJS将功能分成了不同类型的组件分别实现,这些组件有一个统称 ...

  9. Angular.js中的$injector服务

    一 .angular中的依赖注入 angular的一个很重要的特性就是依赖注入,可以分开理解这4个字. 1.依赖: angular里面的依赖,有angular默认提供的,也有我们自己添加的.默认提供的 ...

随机推荐

  1. 转载 jQuery和js自定义函数和文件的方法(全网最全)

    jQuery和js自定义函数和文件的方法(全网最全)    版权声明:本文为像雾像雨又像风_http://blog.csdn.net/topdandan的原创文章,未经允许不得转载. https:// ...

  2. 单例模式和JDBC

    配置文件: driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/blog user=root user=1234 properti ...

  3. WaitForSingleObject的详细用法

    在多线程的情况下,有时候我们会希望等待某一线程完成了再继续做其他事情,要实现这个目的,可以使用Windows API函数WaitForSingleObject,或者WaitForMultipleObj ...

  4. 数据可视化matplotlib、seaborn、pydotplus

    如需转发,请注明出处:小婷儿的python  https://www.cnblogs.com/xxtalhr/p/10486560.html 一.数据可视化 data.mat 链接:https://p ...

  5. AI 蒙特卡罗算法

    蒙特卡罗算法,是一类随机算法,用于求近似解. 1.Las Vegas(拉斯维加斯)算法和蒙特卡罗算法 两者都是随机算法. 前者:要么正确解,要么错误解.采样越多,越有可能得到正确解. 后者:近似解.采 ...

  6. 高显卡安装 低版本的ubuntu系统导致hdmi线不能用

  7. 如何计算PCB设计中的阻抗

    关于阻抗的话题已经说了这么多,想必大家对于阻抗控制在pcb layout中的重要性已经有了一定的了解.俗话说的好,工欲善其事,必先利其器.要想板子利索的跑起来,传输线的阻抗计算肯定不能等闲而视之. 在 ...

  8. 自行实现高性能MVC

    wcf虽然功能多.扩展性强但是也面临配置忒多,而且restful的功能相当怪异,并且目前没法移植.asp.net core虽然支持webapi,但是功能也相对繁多.配置复杂.就没有一个能让码农们安安心 ...

  9. POJ3301 Texas Trip 计算几何、随机化贪心

    传送门--Vjudge 三分写法似乎有问题,可以去Udebug上看Morass的\(666\)个测试点的数据,我的乱搞有很多比正解答案小,但还是能在SPOJ和POJ过,可见数据之水. 可以对正方形的角 ...

  10. .NET Core 3.0 跟踪

    Preview1: https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-3-preview-1-and-ope ...