Angularjs's Dependency Injection method

Source: Internet
Author: User

There are two ways of defining Controller,module,service,and directive,

Way One:

var mymodule = angular.module (' myApp ', []);    Mymodule.controller (function($scope, Project) {    }]);

Way two:

var mymodule = angular.module (' myApp ', []);     function Myctrl ($scope, Project) {    }mymodule.controller (' Myctrl ', Myctrl);

There is no difference in nature between the two approaches, but the second one will cause the pollution of the scope.

You may also realize that the methods of dependency injection in the two definitions are somewhat different, and here is a brief summary:

1. Simple injection method (injection method)

function Myctrl ($scope, Project) {};
Mymodule.controller (' Myctrl ', Myctrl);
// or
Mymodule.controller (function($scope, Project) {
})

ANGULARJS scans the parameters of the function, extracting the name of the parameter (name) as a function dependency,

So this method requires the correctness of the parameter names, but the order of the parameters is not required;

But there is a problem with this injection method, when we publish the project to the formal environment will compress our code, then the function parameters may become a, a, this will lead to problems in our code, the following two kinds of injection methods can help us solve this problem.

2. Array notation (Array-style notation)

function ($scope, Project) {        }])

Each dependent parameter value (string) is stored in an array in the same order, and the value of the array corresponds to the following function parameter one by one, so that there is no problem even if it is compressed.

If you don't like the way the array annotations are defined, here's another way.

3. Display the $inject of the calling function

ANGULARJS provides a way to notify Injector server of the dependencies you want to inject

function Myctrl (A, b) {    //$scope, project, intentionally changed to a A/b after the simulated compression case  = [' $scope ', ' Project '] ; Mymodule.controller (' Phonedetailctrl ', Myctrl);

As above, the effect of dependency injection can be achieved by setting the $inject property of Funciton;

Finally, there is a third-party plug-in ng-min, which can help you to the simple way to inject code into the array of comments automatically, that can satisfy your desire to write concise code, but also to avoid the compression error problem.

Ng-min Address: Https://github.com/btford/ngmin

Interested friends can study the research.

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.