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>
相关文章
相关标签/搜索