Home > Web Front-end > JS Tutorial > Detailed explanation of dependency injection in Angular in one article

Detailed explanation of dependency injection in Angular in one article

青灯夜游
Release: 2022-08-29 20:05:10
forward
2116 people have browsed it

What is dependency injection? This article will give you an in-depth understanding of dependency injection in Angular, I hope it will be helpful to you!

Detailed explanation of dependency injection in Angular in one article

Translator added: Wikipedia points out - In software engineering, dependency injection (Dependency Injection, DI) refers to letting an object receive the dependencies it depends on of other objects. "Dependencies" refer to objects required by the receiver. "Injection" refers to the process of passing "dependencies" to the receiver. After "injection", the receiver will call the "dependency".

Translator added: For example - when classA uses some functions of classB, it means classA Has dependencies on classB. Before using classA, we need to create classB. Recommended articleWhat is Dependency Injection? How to use it?

We all know the standard way of using services services in Angular. Mark the service as injectable and put it in the provider section in the module. [Related tutorial recommendations: "angular tutorial"]

is as follows:

Detailed explanation of dependency injection in Angular in one article

##For dependency injection, we have many tips use. For example, in the module

Angular you can convert a line TestService to a different line of writing.

Detailed explanation of dependency injection in Angular in one article

Every service we put in the

provider section will be converted into an object with two properties. In the provide attribute we can use the class name or we can create a unique key and inject an object. We can even inject a variable.

Detailed explanation of dependency injection in Angular in one article

Another interesting feature is that we can inject multiple objects with the same key and use them like an array.

Detailed explanation of dependency injection in Angular in one article

And, we can also inject different services according to different situations. In other words, we can use the

if-else statement.

Detailed explanation of dependency injection in Angular in one article

Now, I hope you understand the charm of

Angular dependency injection.

If you want to learn more about the relevant code content, please click

here.

This article is a translation, in the form of free translation.

Original address: https://medium.com/geekculture/angular-dependency-injection-7337ecb69aae

For more programming-related knowledge, please visit:

programming video! !

The above is the detailed content of Detailed explanation of dependency injection in Angular in one article. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template