AngularJS $injector 依賴注入詳解
推斷式注入
這種注入方式,需要在保證參數(shù)名稱與服務(wù)名稱相同。如果代碼要經(jīng)過壓縮等操作,就會(huì)導(dǎo)致注入失敗。
app.controller("myCtrl1", function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } });
標(biāo)記式注入
這種注入方式,需要設(shè)置一個(gè)依賴數(shù)組,數(shù)組內(nèi)是依賴的服務(wù)名字,在函數(shù)參數(shù)中,可以隨意設(shè)置參數(shù)名稱,但是必須保證順序的一致性。
var myCtrl2 = function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } } myCtrl2.$injector = ['hello1','hello2']; app.controller("myCtrl2", myCtrl2);
內(nèi)聯(lián)式注入
這種注入方式直接傳入兩個(gè)參數(shù),一個(gè)是名字,另一個(gè)是一個(gè)數(shù)組。這個(gè)數(shù)組的最后一個(gè)參數(shù)是真正的方法體,其他的都是依賴的目標(biāo),但是要保證與方法體的參數(shù)順序一致(與標(biāo)記注入一樣)。
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')獲得依賴的服務(wù)名字
$injector.get('$scope')
通過$injector.annotate('xxx')獲得xxx的所有依賴項(xiàng)
$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>
以上就是對(duì)AngularJS injector的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
相關(guān)文章
Angular使用操作事件指令ng-click傳多個(gè)參數(shù)示例
這篇文章主要介紹了Angular使用操作事件指令ng-click傳多個(gè)參數(shù),結(jié)合實(shí)例形式分析了AngularJS事件指令及相關(guān)的響應(yīng)、處理操作技巧,需要的朋友可以參考下2018-03-03angular 實(shí)時(shí)監(jiān)聽input框value值的變化觸發(fā)函數(shù)方法
今天小編就為大家分享一篇angular 實(shí)時(shí)監(jiān)聽input框value值的變化觸發(fā)函數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08angular框架實(shí)現(xiàn)全選與單選chekbox的自定義
這篇文章主要介紹了angular框架實(shí)現(xiàn)全選與單選chekbox的自定義,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07關(guān)于angular js_$watch監(jiān)控屬性和對(duì)象詳解
下面小編就為大家?guī)硪黄P(guān)于angular js_$watch監(jiān)控屬性和對(duì)象詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04使用Raygun來自動(dòng)追蹤AngularJS中的異常
這篇文章主要介紹了使用Raygun來自動(dòng)追蹤AngularJS中的異常,AngularJS是一款高人氣的JavaScript庫(kù),需要的朋友可以參考下2015-06-06深究AngularJS中ng-drag、ng-drop的用法
本篇文章主要介紹了深究AngularJS中ng-drag、ng-drop的用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06Angular獲取手機(jī)驗(yàn)證碼實(shí)現(xiàn)移動(dòng)端登錄注冊(cè)功能
最近在使用angular來做項(xiàng)目,功能要求實(shí)現(xiàn)一是點(diǎn)擊按鈕獲取驗(yàn)證碼,二是點(diǎn)擊登錄驗(yàn)證表單。之前用jquery來做項(xiàng)目很好做,使用angular怎么實(shí)現(xiàn)呢?其實(shí)實(shí)現(xiàn)代碼也很簡(jiǎn)單的,下面通過實(shí)例代碼給大家介紹下,需要的朋友參考下吧2017-05-05