0 day streak
javascript / beginner
Snippet

Modern Dependency Injection with inject()

The inject() function is the modern Angular way to retrieve dependencies from the DI system without using a constructor. This makes code cleaner and easier to read.

snippet.js
javascript
1
private authService = inject(AuthService);
angular
Breakdown
1
private authService
Defines a private class property to store the service instance.
2
inject(AuthService)
Calls the Angular inject function to provide the AuthService instance.