AngularJS $injector 依賴注入詳解
推斷式注入
這種注入方式,需要在保證參數(shù)名稱與服務(wù)名稱相同。如果代碼要經(jīng)過壓縮等操作,就會導(dǎo)致注入失敗。
app.controller("myCtrl1", function($scope,hello1,hello2){
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
});
標記式注入
這種注入方式,需要設(shè)置一個依賴數(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)式注入
這種注入方式直接傳入兩個參數(shù),一個是名字,另一個是一個數(shù)組。這個數(shù)組的最后一個參數(shù)是真正的方法體,其他的都是依賴的目標,但是要保證與方法體的參數(shù)順序一致(與標記注入一樣)。
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的所有依賴項
$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的資料整理,后續(xù)繼續(xù)補充相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
angular6.0使用教程之父組件通過url傳遞id給子組件的方法
這篇文章主要介紹了angular6.0使用教程之父組件通過url傳遞id給子組件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Angular.js中window.onload(),$(document).ready()的寫法淺析
這篇文章主要介紹了Angular.js中window.onload(),$(document).ready()的寫法淺析,需要的朋友可以參考下2017-09-09
詳解webpack+es6+angular1.x項目構(gòu)建
這篇文章主要介紹了詳解webpack+es6+angular1.x項目構(gòu)建, 小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
AngularJS中如何使用$http對MongoLab數(shù)據(jù)表進行增刪改查
這篇文章主要介紹了AngularJS中如何使用$http對MongoLab數(shù)據(jù)表進行增刪改查的相關(guān)資料,需要的朋友可以參考下2016-01-01
Angular客戶端請求Rest服務(wù)跨域問題的解決方法
本篇文章主要介紹了Angular客戶端請求Rest服務(wù)跨域問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
在Angular中實現(xiàn)一個級聯(lián)效果的下拉框的示例代碼
這篇文章主要介紹了在Angular中實現(xiàn)一個級聯(lián)效果的下拉框的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
如何在Angular8.0下使用ngx-translate進行國際化配置
這篇文章主要介紹了如何在Angular8.0下使用ngx-translate進行國際化配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
淺談angular4 ng-content 中隱藏的內(nèi)容
本篇文章主要介紹了淺談angular4 ng-content 中隱藏的內(nèi)容,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

