国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJS $injector 依賴注入詳解

2019-11-20 09:00:16
字體:
來源:轉載
供稿:網友

推斷式注入

這種注入方式,需要在保證參數名稱與服務名稱相同。如果代碼要經過壓縮等操作,就會導致注入失敗。

 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的資料整理,后續繼續補充相關資料,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 科技| 凌源市| 哈巴河县| 河津市| 武清区| 洛宁县| 棋牌| 休宁县| 桐柏县| 汝南县| 衡东县| 桑日县| 渝中区| 蓬溪县| 新竹市| 潼南县| 太湖县| 安宁市| 阿荣旗| 沾化县| 都安| 晋中市| 义乌市| 湖口县| 岑巩县| 新干县| 东乌珠穆沁旗| 英德市| 溧阳市| 阳高县| 金秀| 嘉义县| 金寨县| 罗甸县| 昌江| 大城县| 固镇县| 鹤岗市| 宁都县| 修水县| 绥中县|