javascript / beginner
Snippet
The @Injectable Decorator
The @Injectable decorator marks a class as available to be injected as a dependency. Using 'providedIn: root' makes the service a singleton available throughout the app.
snippet.js
1
2
3
4
5
6
@Injectable({providedIn: 'root'})export class DataService {getData() { return 'Hello'; }}
angular
Breakdown
1
@Injectable({ ... })
This decorator identifies the class as a service that can be managed by Angular's DI system.
2
providedIn: 'root'
This tells Angular to provide this service at the root level, making it accessible everywhere.