ANGULAR2 Dependency Injection--the use of injectors

Source: Internet
Author: User

First, the display injection device

  Injector = Reflectiveinjector.resolveandcreate ([Car, Engine, Tires]);  Let car = injector.get (car);

Second, a single case service

In the scope of an injector, the dependencies are singleton, because they share a service instance

Three, multi-example service

Angular di is a layered dependency injection system, which means that nested injectors can create their own service instances.

Iv. Role of @injectabled

Identifies a class that can be instantiated by an injector. Typically, when you attempt to instantiate a class that is not identified @Injectable() , the injector will give an error.

It is recommended for each service class to @Injectable() be added, including those that do not rely strictly and do not need it. Because:

      • Future-oriented: no need to remember to add dependencies later @Injectable() .

      • Consistency: All services follow the same rules and do not need to consider why one is missing somewhere.

Five, @Component, @Directive, @Pipe do not need to add adorners, because they are a subtype of @injectable.

ANGULAR2 Dependency Injection--the use of injectors

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.