Skip to main content

Providers and the injector hierarchy

Learn Providers and the injector hierarchy (Module 8 · Services and Dependency Injection, Angular) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Provider configuration and providedIn: 'root' • A provider tells the injector how to create a token. The shortest provider is the class itself. With @Injectable({ providedIn: 'root' }) the service registers in the root injector : one instance for the whole app, created only if somebody uses it (so unused services are tree-shaken out of the bundle). This is the default choice for most services.

More lessons in Module 8 · Services and Dependency Injection