Dependency injection
Last modified on Thu 23 Jan 2025
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. Alongside this, prefer to inject dependencies using the inject
function over constructor
.
// OK, but not preferred
constructor(private readonly userService: UserService){}
// Preferred way
private readonly userService = inject(UserService);
DI is very useful for testing purposes, as shown in a later chapter.