Dependency injection
Last modified on Mon 04 Jan 2021
Dependency Injection (DI) is utilized heavily in Angular, primarily for providing instances of either primitive values or class instances.
DI is a complex topic that requires a separate discussion which would be out of the scope of this handbook. We recommend checking out the following resources:
- The official DI guide which explains many details
- This repo for some examples
- This useful infographic that gives a good graphical overview of the whole DI mechanism
As of Angular 6, make sure to use { providedIn: 'root' }
for singletons whenever possible.
DI is very useful for testing purposes, as shown in a later chapter.