码迷,mamicode.com
首页 > Web开发 > 详细

angularJs $injector

时间:2016-09-05 10:30:48      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

一 angularJS中几种注入方式
Spring中使用构造注入或者设值注入的方式,还需要做一些额外的操作,但是angular中只需要在需要的地方声明一下即可,类似模块的引用,因此十分方便。
angularJS中有几种注入方式:
推断式注入:这种注入方式,需要在保证参数名称与服务名称相同。如果代码要经过压缩等操作,就会导致注入失败。
     <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");
            }
        }
    });
 
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();
        }
    }]);
在angular中,可以通过angular.injector()获得注入器:var $injector = angular.injector();
通过$injector.get(‘serviceName‘)获得依赖的服务名字:$injector.get(‘$scope‘);
通过$injector.annotate(‘xxx‘)获得xxx的所有依赖项 :$injector.annotate(xxx)
二 angularJS中的$injector、$rootScope和$scope
1.$injector其实是一个IOC容器,包含了很多服务(类似于spring框架中的bean),其它代码能够通过。$injector.get("serviceName")的方式,从injector中获取所需要的服务。
2.scope是angularJS中的作用域(其实就是存储数据的地方),很类似javascript的原型链。搜索的时候,优先找自己的scope,如果没有找到就沿着作用域链向上搜索,直至到达根作用域rootScope。
3.$rootScope 是由angularJS加载模块的时候自动创建的,每个模块只会有1个rootScope。rootScope创建好会以服务的形式加入 到$injector中。也就是说通过$injector.get("$rootScope");能够获取到某个模块的根作用域。更准确的来 说,$rootScope是由angularJS的核心模块ng创建的。
4.$rootScope的确是由核心模块ng创建的,并以服务的形式存在于injector中。
     // 新建一个模块 
    var module = angular.module("app",[]); 
 
    // true说明$rootScope确实以服务的形式包含在模块的injector中 
    var hasNgInjector = angular.injector([‘app‘,‘ng‘]);   
    console.log("has $rootScope=" + hasNgInjector.has("$rootScope"));//true 
 
    // 获取模块相应的injector对象,不获取ng模块中的服务 
    // 不依赖于ng模块,无法获取$rootScope服务 
    var noNgInjector = angular.injector([‘app‘]); 
    console.log("no $rootScope=" + noNgInjector.has("$rootScope"));//false 
 
    // 获取angular核心的ng模块 
    var ngInjector = angular.injector([‘ng‘]);   
    console.log("ng $rootScope=" + ngInjector.has("$rootScope"));//true
如果创建injector的时候,指定了ng模块,那么该injector中就会包含$rootScope服务;否则就不包含$rootScope。 
<html lang="en"> 
    <head> 
       <meta charset="utf-8"> 
       <script src="angular-1.2.25.js"></script> 
       <script> 
        var module = angular.module("app",[]); 
        // 控制器里的$injector,是由angular框架自动创建的 
        function FirstController($scope,$injector,$rootScope) 
        { 
            $rootScope.name="aty"; 
        } 
        //自己创建了个injector,依赖于app和ng模块 
        var myInjector = angular.injector(["app","ng"]); 
        var rootScope = myInjector.get("$rootScope"); 
        alert(rootScope.name);//udefined 
       </script>   
    </head> 
    <body ng-app="app"> 
        <div id="first" ng-controller="FirstController"> 
            <input type="text" ng-model="name"> 
            <br> 
            {{name}} 
        </div>     
    </body> 
</html> 
angular.injector()可以调用多次,每次都返回新建的injector对象。所以我们自己创建的myInjector和angular自动创建的$injector不是同一个对象,那么得到的rootScope也就不是同一个。
<html lang="en"> 
    <head> 
       <script src="angular-1.2.25.js"></script> 
       <script> 
        function FirstController($scope,$injector,$rootScope) 
        { 
            // true 
            console.log("scope parent :" + ($scope.$parent ==$rootScope)); 
        } 
       </script>   
    </head> 
    <body ng-app> 
        <div id="first" ng-controller="FirstController"> 
            <input type="text" ng-model="name"> 
            <br> 
            {{name}} 
        </div>     
    </body> 
</html> 
ng-controller指令给所在的DOM元素创建了一个新的$scope对象,并作为rootScope的子作用域。$scope是由$rootScope创建的,$scope不会包含在$injector中。
 

angularJs $injector

标签:

原文地址:http://www.cnblogs.com/lyy-2016/p/5841246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!