← Angular learning path
Angular · Module 5 of 10

Services and dependency injection

Share logic and data across components with injectable services.

📖 Key ideas

  1. A service is a class for shared logic: data, API calls, utilities.
  2. @Injectable({ providedIn: "root" }) makes one shared instance.
  3. Inject with the constructor or inject(DataService).
  4. Generate: ng g s data.
  5. 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