Dependency Injection is a way of architecting an application so code is easier to
It 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 dependant pieces of code is handled by something else.
Angular comes with a Dependency Injection (DI) framework of it’s own and it’s used throughout Angulars 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 4 concepts working together:
This uniquely identifies something that we want injected. A dependancy of our code.
The actual code we want injected.
This is a map between a token and a list of dependancies.
This is a function which when passed a token returns a dependancy (or a 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 4 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
If we want a separate instance of a dependency to be shared across each instance of a component and it’s children we configure it on the components
If we want a separate instance of a dependency to be shared across each instance of a component and only it’s view children we configure it on the components