亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

AngularJS $injector 依賴注入詳解

 更新時間:2016年09月14日 14:46:42   作者:xingoo  
這篇文章主要介紹了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);

內聯(lián)式注入

這種注入方式直接傳入兩個參數,一個是名字,另一個是一個數組。這個數組的最后一個參數是真正的方法體,其他的都是依賴的目標,但是要保證與方法體的參數順序一致(與標記注入一樣)。

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

相關文章

  • Angular使用操作事件指令ng-click傳多個參數示例

    Angular使用操作事件指令ng-click傳多個參數示例

    這篇文章主要介紹了Angular使用操作事件指令ng-click傳多個參數,結合實例形式分析了AngularJS事件指令及相關的響應、處理操作技巧,需要的朋友可以參考下
    2018-03-03
  • angular 實時監(jiān)聽input框value值的變化觸發(fā)函數方法

    angular 實時監(jiān)聽input框value值的變化觸發(fā)函數方法

    今天小編就為大家分享一篇angular 實時監(jiān)聽input框value值的變化觸發(fā)函數方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Angular彈出模態(tài)框的兩種方式

    Angular彈出模態(tài)框的兩種方式

    這篇文章主要介紹了Angular彈出模態(tài)框的兩種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-10-10
  • angular框架實現全選與單選chekbox的自定義

    angular框架實現全選與單選chekbox的自定義

    這篇文章主要介紹了angular框架實現全選與單選chekbox的自定義,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 關于angular js_$watch監(jiān)控屬性和對象詳解

    關于angular js_$watch監(jiān)控屬性和對象詳解

    下面小編就為大家?guī)硪黄P于angular js_$watch監(jiān)控屬性和對象詳解。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 使用Raygun來自動追蹤AngularJS中的異常

    使用Raygun來自動追蹤AngularJS中的異常

    這篇文章主要介紹了使用Raygun來自動追蹤AngularJS中的異常,AngularJS是一款高人氣的JavaScript庫,需要的朋友可以參考下
    2015-06-06
  • AngularJS的臟檢查深入分析

    AngularJS的臟檢查深入分析

    這篇文章主要介紹了AngularJS的臟檢查深入分析,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • AngularJS入門教程之更多模板詳解

    AngularJS入門教程之更多模板詳解

    本文主要介紹AngularJS模板的資料知識,這里幫大家整理了詳細的模版資料,及實現示例代碼,幫助大家學習AngularJS的知識,有需要的小伙伴可以參考下
    2016-08-08
  • 深究AngularJS中ng-drag、ng-drop的用法

    深究AngularJS中ng-drag、ng-drop的用法

    本篇文章主要介紹了深究AngularJS中ng-drag、ng-drop的用法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Angular獲取手機驗證碼實現移動端登錄注冊功能

    Angular獲取手機驗證碼實現移動端登錄注冊功能

    最近在使用angular來做項目,功能要求實現一是點擊按鈕獲取驗證碼,二是點擊登錄驗證表單。之前用jquery來做項目很好做,使用angular怎么實現呢?其實實現代碼也很簡單的,下面通過實例代碼給大家介紹下,需要的朋友參考下吧
    2017-05-05

最新評論