English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

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);

内联式注入

这种注入方式直接传入两个参数,一个是名字,另一个是一个数组。这个数组的最后一个参数是真正的方法体,其他的都是依赖的目标,但是要保证与方法体的参数顺序一致(与标记注入一样)。

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));//참
  console.log(angular.equals($injector.invoke(function($injector) {return $injector;}),$injector));//참
  //추론된
  // $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에 대한 자료 정리입니다. 이어서 관련 자료를 계속 추가하겠습니다. 많은 관심과 지원에 감사합니다!

고지: 이 문서의 내용은 인터넷에서 가져왔으며, 저작권자는 모두에게 있습니다. 내용은 인터넷 사용자가 자발적으로 기여하고 업로드한 것이며, 이 사이트는 소유권을 가지지 않으며, 인공적으로 편집된 것도 아니며, 관련 법적 책임도 부담하지 않습니다. 저작권 위반 내용이 발견되면, notice#w로 이메일을 보내 주세요.3codebox.com에 대한 신고를 보내시면, #을 @으로 변경하시고 관련 증거를 제공해 주세요. 실제로 확인되면, 이 사이트는 즉시 위반된 내용을 삭제합니다.

당신이 좋아할 만한 것