AngularJS dependency Injection Method

Source: Internet
Author: User

AngularJS dependency Injection Method
There are two methods to define controller, module, service, and directive. Method 1: var myModule = angular. module ('myapp', []); myModule. controller ('myctrl ', [' $ scope ', 'project', function ($ scope, Project) {}]); Method 2: var myModule = angular. module ('myapp', []); function myCtrl ($ scope, Project) {} myModule. controller ('myctrl ', myCtrl); these two methods are essentially no different, but method 2 will cause scope pollution. You may also realize that the dependency injection methods in the preceding two definition methods are also different. The following is a brief summary: 1. simple injection method function myCtrl ($ scope, Project) {}; myModule. controller ('myctrl ', myCtrl); // or myModule. controller (function ($ scope, Project) {}) AngularJs will scan function parameters and extract the parameter name (name) as the function dependency, therefore, this method requires that the parameter name be correct, but there is no requirement on the parameter order. However, this injection method has a problem, when we release the project to the official environment, our code will be compressed. At this time, the function parameter may become a, B, which will cause problems in our code, the following two injection methods can help us solve this problem. 2. array-style notation myModule. controller ('myctrl ', [' $ scope ', 'preobject', function ($ scope, Project) {}]) parameter values of each dependency (string) all are stored in an array in the same order. The values of the array correspond to the following function parameters one by one, so that even if the data is compressed, there will be no problem. If you do not like the definition of this array annotation method, there is another method below. 3. the $ inject AngularJs that displays the function provides a function myCtrl (a, B) {// $ scope, Project, deliberately changed to a and B to simulate compression.} myCtrl. $ inject = ['$ scope', 'project']; myModule. controller ('phonedetailctrl ', myCtrl); As shown above, dependency injection can be achieved by setting the $ inject attribute of funciton. Finally, there is a third-party plug-in ng-min, it can help you automatically convert the code injected in a simple way into an array comment, which can satisfy your desire to write concise code and avoid compression errors.

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.