AngularJs $injector

Source: Internet
Author: User

Several injection methods in a angularjs the use of construct injection or set value injection in spring requires some additional action, but it is very convenient to simply declare it in the angular where it is needed, like a reference to a module. There are several injection methods in ANGULARJS: Inferred injection: This injection method requires the same parameter name as the service name. If the code is compressed and so on, it will cause the injection to fail.      <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 () {            &NBSP   Console.log ("Hello1 service");           }       }    });    app.factory ("Hello2", function () {        return {          & nbsp Hello:function () {                Console.log ("Hello2 service");    &nbs P      }       }   });  app.controller ("MyCtrl1", Function ($scope, Hello1,hello2) {        $scope. Hello = function () {            Hello1.hell O ();            Hello2.hello ();       }   }); Tagged injection: This type of injection requires an array of dependencies, a dependent service name within the array, and in the function arguments, the parameter names can be set arbitrarily, but the order must be consistent. var myCtrl2 = function ($scope, Hello1,hello2) {        $scope. Hello = function () {      &N Bsp     Hello1.hello ();            Hello2.helLo ();       }   }    myCtrl2. $injector = [' Hello1 ', ' Hello2 '];    APP.C Ontroller ("MyCtrl2", myCtrl2); inline injection: This injection method directly passes in 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 ();          &NBSP ; Hello2.hello ();       }   }]); in angular, the injector can be obtained by Angular.injector (): var $injector = Angular.injector (); Get dependent service name by $injector.get (' ServiceName '): $injector. Get (' $scope '); via $injector.annotate (' xxx ') Get all the dependencies for XXX: $injector. Annotate (XXX) Two  angularjs in $injector, $rootScope, and $scope1. $injector is actually an IOC container that contains a lot of services ( Similar to the bean in the spring Framework), other code can pass. $injector. Get ("ServiceName") way to get the required services from the injector. 2.scope is the scope in Angularjs (in fact, where data is stored), much like the prototype chain of JavaScript. When searching, prefer to find their own scope, if not found on the scope chain up search, until the root scope rootsCope 3. $rootScope is created automatically by Angularjs when the module is loaded, each module will only have 1 rootscope. Rootscope is created as a service and is added to the $injector in the form of services. That is, through $injector.get ("$rootScope"), the ability to obtain the root scope of a module. More precisely, the $rootScope was created by Angularjs's core module NG. 4. The $rootScope is indeed created by the core module NG and exists as a service in injector.      //Create a new module      var module = angular.module ("App", []);    & nbsp True indicates that $rootscope is indeed included in the injector of the module in the form of a service      var hasnginjector = Angular.injector ([' app ', ' ng ']);        Console.log ("has $rootScope =" + Hasnginjector.has ("$rootScope"));//true     //Get module corresponding Injector object, do not get service in NG module     //Do not rely on NG module, cannot get $rootscope service      var Nonginjector = Angular.injector ([' app ']);     Console.log ("no $rootScope =" + Nonginjector.has ("$ Rootscope "));//false     //Get angular core ng module      var nginjector = Angular.injector ([' ng ']);       Console.log ("ng $rootScope =" + nginjector. has ("$rootScope"));//true If a NG module is specified when the injector is created, the $rootscope service is included in the injector, otherwise it does not contain $rootscope.  

AngularJs $injector

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.