Sub topic 8
The Injector Hierarchy
This chapter assumes you know how to create a service with @Injectable, inject it with inject(), and register providers. Angular has many injectors arranged in a tree, not one. When a component asks for a dependency, Angular searches that tree in a fixed order. If you know the order, you can predict which instance the component receives.
Two Kinds of Injectors
Angular keeps two separate injector trees.
| Injector type | Created by | Typical providers |
|---|---|---|
| Environment injector | `bootstrapApplication`, `providedIn: 'root'`, route `providers`, `createEnvironmentInjector` | App-wide services, configuration, HTTP, router |
| Element injector | A component or directive that has `providers` or `viewProviders` | Services tied to one part of the DOM |
The environment tree has these levels, from top to bottom:
- Null injector. It sits at the top and holds nothing. Reaching it means the lookup failed, and Angular throws
NullInjectorError. - Platform injector. It holds services shared by every Angular app on the page. You rarely add anything here.
- Root environment injector. It holds everything from
bootstrapApplicationproviders and everyprovidedIn: 'root'service. - Child environment injectors. Angular creates one for each route that declares
providers, including lazy-loaded routes.
Element injectors follow the component tree. Each component or directive that declares providers gets its own element injector. Its parent is the element injector of the nearest ancestor element.
How Resolution Works
When code calls inject(SomeService), Angular runs two phases:
- Element phase. It starts at the requesting element's injector and walks up through the ancestor elements until it reaches the root component.
- Environment phase. If no element injector had a provider, Angular moves to the environment injector for that part of the app, usually a route's injector or the root. It then walks up through the root and platform injectors to the null injector.
The first provider Angular finds wins. A provider lower in the tree hides any provider for the same token higher up.
Route-Level Environment Injectors
Adding providers to a route creates a child environment injector for that route and the routes under it. Components rendered by those routes get the route's instance. Components outside that route never see it.
import { Injectable } from '@angular/core';
import { Routes } from '@angular/router';
@Injectable()
export class AdminAuditService {
readonly entries: string[] = [];
record(action: string): void {
this.entries.push(action);
}
}
export const routes: Routes = [
{
path: 'admin',
providers: [AdminAuditService],
loadComponent: () =>
import('./admin/admin.component').then(m => m.AdminComponent)
}
];AdminAuditService is not providedIn: 'root'. Code outside /admin that injects it gets a NullInjectorError. Inside /admin, every component shares one instance, and that instance lives as long as the route's injector.
Common Errors
- Expecting a fresh instance and getting a shared one. A
providedIn: 'root'service is a singleton for the whole app, even if a component also lists it in itsproviders. In that case the component gets its own instance, and every other consumer still shares the root instance. NullInjectorErrorafter moving a provider. If you move a provider frombootstrapApplicationto a route, consumers outside that route can no longer find it.- Environment services injecting element services. A root service cannot inject a service that only a component provides. Environment injectors never search element injectors.
Exercise
A component inside the /admin route calls inject(Logger). Logger is provided in its parent component's providers, in the /admin route's providers, and in bootstrapApplication. Which instance does it receive, and why?
Show answer
It receives the parent component's instance. Angular searches element injectors first, walking up from the requesting component. The parent's element injector has a provider for Logger, so the search stops there and never reaches the route or root environment injectors.
Exercise
A service marked providedIn: 'root' calls inject(FormDraftService), and FormDraftService is listed only in a component's providers. What happens?
Show answer
Angular throws NullInjectorError. The root service resolves its dependencies through the environment injector tree, which never checks element injectors. The lookup continues up to the null injector and fails.