Understanding of angular Dependency Injection (RPM)

Source: Internet
Author: User

People who have used Java for development are sure to know the famous spring framework and certainly have some understanding of the spring IOC, and if you need to use these beans after you have defined the beans through the configuration file, you do not need to instantiate them, but with the large container of spring. Our angular framework also implements this mechanism.

Consider, if the object needs to gain control over its dependencies, what are the different ways?

1. Create a dependent instance within the object itself

2. Define the dependency as global and then refer to it through the global variables

3. Pass through parameters where needed

Dependency injection is implemented in a third way, and the hard coding of dependencies can be done through dependency injection.

Let's take a look at an example of how dependency injection is used in angular.

Angular.module (' Test ', []). Controller (' TestController ', function ($scope, $location) {})

We register a controller for the module, the controller accepts two parameters $scope and $location, these two parameters are angular built-in services, then the controller is called when these services are injected into it?

Queries and instantiations of dependencies are managed in angular through the $injector service.

Inferred injection declarations in the example above, there is no declaration, angularjs that the name of the parameter is the name of the dependency, angular is found in the registered service based on the name of the parameter, and then injects these parameters into the instance object by $injector

Injector.invoke (function ($scope, $location) {})

Because this is injected based on the name of the parameter, the order of the parameters is not related.

However, in the production environment, in order to shorten the loading time of the Web page, we usually compress the JS file, the name of the parameter will be replaced by the alias, this time by the parameter name on the line injection is not feasible.

Show injection declarations

By displaying the method to clearly define the function's dependencies, even if the source code is compressed, it will work. Injected by the $inject property to display. The $inject property of a function object is an array whose element is a string whose value is the name of the service that needs to be injected.

Angular.module (' Test ', []). Controller (' TestController ', testcontroller); function TestController ($scope, $location) {}testcontroller. $injector = [' $scope ', ' $location ']
In-line injection declarations

The injection declaration in the line is actually the same as the injection declaration effect, except that when the function is defined, the arguments are passed in from within the row, avoiding the use of temporary variables during use.

Angular.module (' Test ', []). Controller (' TestController ', [' $scope ', ' $location ', function ($scope, $location) {}])

Understanding of angular Dependency Injection (RPM)

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.