#native_company# #native_desc#
#native_cta#

Wrapping Up

Dependency Injection is a way of architecting an application so code is easier to re-use, easier to test and easier to maintain.

It’s a method of decoupling a piece of code from the dependencies it needs in order to run.

It uses Inversion of Control so the responsibility of creating dependencies and and passing them in to dependent pieces of code is handled by something else.

Angular comes with a Dependency Injection (DI) framework of its own and it’s used throughout Angular’s code.

To understand how to use Angular and architect your application you need to have a very good understanding of the DI framework.

The DI framework in Angular consists of four concepts working together:

Token

This uniquely identifies something that we want injected. A dependency of our code.

Dependency

The actual code we want injected.

Provider

This is a map between a token and a list of dependencies.

Injector

This is a function which when passed a token returns a dependency (or list of dependencies).

We configure injectors with providers, Angular then uses these injectors to resolve dependencies using tokens and injecting them into constructors as arguments.

There are four types of providers: a class provider, a value provider, a factory function provider and an alias provider.

If we want a dependency to be shared across our entire application we would configure it on our NgModule.

If we want a separate instance of a dependency to be shared across each instance of a component and its children, we configure it on the component’s providers property.

If we want a separate instance of a dependency to be shared across each instance of a component and only its view children, we configure it on the component’s viewProviders property.


Caught a mistake or want to contribute to the book? Edit this page on GitHub!



Advanced JavaScript

This unique course teaches you advanced JavaScript knowledge through a series of interview questions. Bring your JavaScript to the 2021's today.

Level up your JavaScript now!