0 day streak
javascript / beginner
Snippet

Dependency Injection Basics

Dependency Injection (DI) is a core pattern where a class requests dependencies from external sources rather than creating them. In Angular, you inject services through the constructor.

snippet.js
javascript
1
2
3
export class UserComponent {
constructor(private userService: UserService) {}
}
angular
Breakdown
1
constructor(...)
The constructor is the place where you declare which dependencies the component needs.
2
private userService: UserService
The 'private' keyword tells Angular to automatically create a property and assign the injected service to it.