← Angular learning pathAngular · Module 5 of 10
Services and dependency injection
Share logic and data across components with injectable services.
📖 Key ideas
- A service is a class for shared logic: data, API calls, utilities.
- @Injectable({ providedIn: "root" }) makes one shared instance.
- Inject with the constructor or inject(DataService).
- Generate: ng g s data.
- Keeps components small and focused on the view.
💡 Real-life example: Dependency injection is a hotel concierge: you ask for a taxi and one is provided, you don't build the car.
ServiceClass with shared logic
@InjectableMarks a class for DI
providedIn: rootOne app-wide instance
inject()Function that gets a dependency