AngularJS DI (Dependency injection) implementation speculation

Source: Internet
Author: User

AngularJS DI (Dependency injection)

Http://www.cnblogs.com/whitewolf/archive/2012/09/11/2680659.html

Back to Angularjs: the Angular.injector (modules) Di injection syringe is provided for us in the framework. But when we use injection, we often don't need to be concerned about how to inject it. We just have to write our Angularjs code according to its rules and it's easy to get Angularjs's di feature.

1: Inferred injection: In Angularjs we can inject by name where we need to inject, requiring that the parameter name must be the same as the injected service instance name, a name convention. ANGULARJS extracts the parameter name to find the corresponding DI instance injection.

For example:

var mymodule = angular.module (' MyModule ', []) mymodule.factory (' $alert ', function ($window) {    return {        alert: function (text) {            $window. alert (text);        }    };}); var mycontroller = function ($scope, $alert) {    $scope. message = function (msg) {        console.log (msg);        $alert. alert (msg);};    Mymodule.controller ("Mycontroller", Mycontroller);?

Analysis

The controller Mycontroller is a function that has a $scope, $alert two parameters,

The first of these two parameters is the app domain and the second is the dependent service, which is now the $alert service defined by Mymodule.factory.

The Mycontroller controller is initialized at the time of the HTML declaration, even if it is called. The following HTML

<ng-app="MyModule"  ng-controller=ng-init=<ng-model="MSG" >  <ng-click="message (msg);" >click me</<br/> {{msg}}</div>      

Do not see any call mycontroller Place, how to control incoming parameter? and $alert parameters can correspond to the $alert service defined by Mymodule.factory??

Guessing is the call flow that the framework implements and injects the service into the Mycontroller controller when the controller is initialized.

Specifically, the JS function definition has an introspective nature, uses the tostring method, resolves the parameter name, and then finds the service that corresponds to the parameter name in the service list, and then calls the Mycontroller method.

JS's introspection test is as follows:

function test ($scope, $alert) {console.log ("AAA")}

Test.tostring (). Match (/\ ([^\ (\)]*\)/) ["($scope, $alert)", index:13, Input: "Function test ($scope, $alert) { Console.log ("AAA")} ", Groups:undefined]0:" ($scope, $alert) "Groups:undefinedindex:13input:" Function test ($scope, $ Alert) {Console.log ("AAA")} "length:1__proto__: Array (0)
Test.tostring (). Match (/\ ([^\ (\)]*\)/) [0] "($scope, $alert)"

Reference:

https://segmentfault.com/q/1010000002581288

AngularJS DI (Dependency injection) implementation speculation

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.