AngularJS API's $injector----Dependency Injection

Source: Internet
Author: User

Read Catalogue

    • Inferred injection
    • Tagged injection
    • Inline-Injection
    • $injector Common methods
    • Sample code

There are also concepts of dependency injection in Angularjs, like dependency injection in spring, but different. There are some additional things you need to do to use construct injection or set value injection in spring, but it is convenient to just declare it where you need it, like a reference to a module, in angular.
Reference: [Angular API Doc] (http://docs.angularjs.cn/api/auto/service/$injector)

Back to top inferred injection

This injection method requires that the parameter name be the same as the service name. If the code is compressed and so on, it will cause the injection to fail.

App.controller ("MyCtrl1", Function ($scope, Hello1,hello2) {        $scope. Hello = function () {            Hello1.hello ();            Hello2.hello ();        }    });

Back to top marker injection

This kind of injection method, need to set up a dependent array, the array is dependent on the service name, in the function parameters, you can set the parameter name arbitrarily, but must ensure the consistency of the order.

var myCtrl2 = function ($scope, Hello1,hello2) {        $scope. Hello = function () {            Hello1.hello ();            Hello2.hello ();        }    }    MyCtrl2. $injector = [' Hello1 ', ' Hello2 '];    App.controller ("MyCtrl2", MYCTRL2);

Back to top inline-injection

This injection method directly passes two parameters, one is the name and the other is an array. The last parameter of this array is the true method body, and the others are dependent targets, but ensure that the parameter order of the method body is consistent (as with the tag injection).

App.controller ("MyCtrl3", [' $scope ', ' hello1 ', ' Hello2 ', function ($scope, Hello1,hello2) {        $scope. Hello = function () {            Hello1.hello ();            Hello2.hello ();        }    }]);

Back to the top $injector commonly used methods in angular, can be angular.injector()Get the injector. 
var $injector = Angular.injector ();

through $injector.get(‘serviceName‘) get a dependent service name

$injector. Get (' $scope ')

By $injector.annotate(‘xxx‘) getting all the dependencies of XXX

$injector. Annotate (XXX)

Back to the top sample code
<HTML><Head>    <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />    <Scriptsrc= "Http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></Script></Head><BodyNg-app= "MYAPP">    <DivNg-controller= "MYCTRL1">        <inputtype= "button"Ng-click= "Hello ()"value= "CTRL1"></input>    </Div>    <DivNg-controller= "MyCtrl2">        <inputtype= "button"Ng-click= "Hello ()"value= "Ctrl2"></input>    </Div>    <DivNg-controller= "MyCtrl3">        <inputtype= "button"Ng-click= "Hello ()"value= "Ctrl3"></input>    </Div>    <Scripttype= "Text/javascript">    varapp=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));//trueConsole.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);    varMyCtrl2= function($scope, Hello1,hello2) {$scope. Hello= function() {Hello1.hello ();        Hello2.hello (); }} myCtrl2. $injector= ['Hello1','Hello2']; App.controller ("MyCtrl2", MYCTRL2); //inlineApp.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 API's $injector----Dependency Injection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.