AngularJS $injector(依賴注入)

在AngularJS中也有依賴注入的概念,像spring中的依賴注入,可是又有所不一樣。Spring中使用構造注入或者設值注入的方式,還須要作一些額外的操做,可是angular中只須要在須要的地方聲明一下便可,相似模塊的引用,所以十分方便。
參考:[angular api doc] (http://docs.angularjs.cn/api/auto/service/$injector)javascript

推斷式注入

這種注入方式,須要在保證參數名稱與服務名稱相同。若是代碼要通過壓縮等操做,就會致使注入失敗。html

app.controller("myCtrl1", function($scope,hello1,hello2){
        $scope.hello = function(){
            hello1.hello();
            hello2.hello();
        }
    });

標記式注入

這種注入方式,須要設置一個依賴數組,數組內是依賴的服務名字,在函數參數中,能夠隨意設置參數名稱,可是必須保證順序的一致性。java

var myCtrl2 = function($scope,hello1,hello2){
        $scope.hello = function(){
            hello1.hello();
            hello2.hello();
        }
    }
    myCtrl2.$injector = ['hello1','hello2'];
    app.controller("myCtrl2", myCtrl2);

內聯式注入

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

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>
相關文章
相關標籤/搜索