AngularJS $injector 依赖注入详解
2019/6/27 22:08:53
本文主要是介绍AngularJS $injector 依赖注入详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
推断式注入
这种注入方式,需要在保证参数名称与服务名称相同。如果代码要经过压缩等操作,就会导致注入失败。
app.controller("myCtrl1", function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } });
标记式注入
这种注入方式,需要设置一个依赖数组,数组内是依赖的服务名字,在函数参数中,可以随意设置参数名称,但是必须保证顺序的一致性。
var myCtrl2 = function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } } myCtrl2.$injector = ['hello1','hello2']; app.controller("myCtrl2", myCtrl2);
内联式注入
这种注入方式直接传入两个参数,一个是名字,另一个是一个数组。这个数组的最后一个参数是真正的方法体,其他的都是依赖的目标,但是要保证与方法体的参数顺序一致(与标记注入一样)。
app.controller("myCtrl3",['$scope','hello1','hello2',function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } }]);
$injector常用的方法
在angular中,可以通过angular.injector()获得注入器。
var $injector = angular.injector();
通过$injector.get('serviceName')获得依赖的服务名字
$injector.get('$scope')
通过$injector.annotate('xxx')获得xxx的所有依赖项
$injector.annotate(xxx)
样例代码
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script> </head> <body ng-app="myApp"> <div ng-controller="myCtrl1"> <input type="button" ng-click="hello()" value="ctrl1"></input> </div> <div ng-controller="myCtrl2"> <input type="button" ng-click="hello()" value="ctrl2"></input> </div> <div ng-controller="myCtrl3"> <input type="button" ng-click="hello()" value="ctrl3"></input> </div> <script type="text/javascript"> var app = angular.module("myApp",[]); app.factory("hello1",function(){ return { hello:function(){ console.log("hello1 service"); } } }); app.factory("hello2",function(){ return { hello:function(){ console.log("hello2 service"); } } }); var $injector = angular.injector(); console.log(angular.equals($injector.get('$injector'),$injector));//true console.log(angular.equals($injector.invoke(function($injector) {return $injector;}),$injector));//true //inferred // $injector.invoke(function(serviceA){}); app.controller("myCtrl1", function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } }); //annotated // function explicit(serviceA) {}; // explicit.$inject = ['serviceA']; // $injector.invoke(explicit); var myCtrl2 = function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } } myCtrl2.$injector = ['hello1','hello2']; app.controller("myCtrl2", myCtrl2); //inline app.controller("myCtrl3",['$scope','hello1','hello2',function($scope,hello1,hello2){ // app.controller("myCtrl3",['$scope','hello1','hello2',function(a,b,c){ // a.hello = function(){ // b.hello(); // c.hello(); // } $scope.hello = function(){ hello1.hello(); hello2.hello(); } }]); console.log($injector.annotate(myCtrl2));//["$scope","hello1","hello2"] </script> </body> </html>
以上就是对AngularJS injector的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
这篇关于AngularJS $injector 依赖注入详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist