추론 된 주사
이 주입 방법을 사용하려면 매개 변수 이름이 서비스 이름과 동일하도록해야합니다. 코드를 압축 해야하는 경우 주입이 실패합니다.
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. $ 인젝터 = [ 'hello1', 'hello2']; app.controller ( "myctrl2", myctrl2);인라인 주입
이 주입 방법은 두 매개 변수로 직접 전달됩니다. 하나는 이름이고 다른 하나는 배열입니다. 이 배열의 마지막 매개 변수는 실제 메소드 본문이고 나머지는 모두 종속성이지만 메소드 본문의 매개 변수가 동일한 순서 (마크 주입과 동일)를 보장해야합니다.
app.controller ( "myctrl3", [ '$ scope', 'hello1', 'hello2', function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}}];$ 인젝터의 일반적인 방법
각도에서, 인젝터는 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"> <div ng-controller = "myctrl1"> <input type = "button"ng-click = "hello ()"value = "value ="ctrl1 "> </input> </div> <div ng-controller ="myctrl2 "> <input type ="ng-click = "hello ()"value = "ctrl2"> </input> </input> 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 ($ injector) {return $ injector;}), $ injector); // true // $ injector.invoke (service); app.controller ( "myctrl1", function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}}); // 주석이 달린 // function explicit (servicea) {}; // 명시 적. $ inject = [ 'servicea']; // $ injector.invoke (명시 적); var myctrl2 = function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello (); }} myctrl2. $ 인젝터 = [ '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); c.hello (); hello.hello ();위는 AngularJS 인젝터의 정보를 편집 한 것입니다. 우리는 향후 관련 정보를 계속 추가 할 것입니다. 이 웹 사이트를 지원 해주셔서 감사합니다!