Services, Dependency Injection and Providers
Learn Services, Dependency Injection and Providers (Phase 5 · Services and Dependency Injection (Essential), Angular) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
Why services? • Components should be thin: they display data and handle user events. Services hold reusable logic and data access: HTTP calls, authentication, caching, shared state, logging, business rules. A service is a plain TypeScript class marked @Injectable() . • Dependency injection (DI) : a component asks for a service and Angular's injector creates and supplies it. This is the same idea as Spring's IoC container with @Autowired constructor injection. • Generate with ng g service core/course .