Sub topic 4
Two hierarchies: environment injectors and element injectors
This chapter assumes you know TypeScript decorators, standalone components, basic services, and route configuration. It explains how Angular organizes injectors into two connected trees. Later chapters cover each tree in detail.
Why there are two trees
An injector is an object that holds providers and creates dependencies on request. Angular does not use a single injector for the whole application. It uses two hierarchies:
- Environment injectors hold providers that belong to the application or to a part of it, such as a route. They are not tied to the DOM.
- Element injectors are created for elements in a template that have a component or directive. They follow the component tree.
| Row | Environment injector | Element injector |
|---|---|---|
| Follows | Application structure (platform, root, routes) | Component and directive tree in the DOM |
| Configured with | `providedIn`, `bootstrapApplication`, route `providers` | `providers` and `viewProviders` on a component or directive |
| Typical lifetime | Application or route | The component instance |
| Typical use | Shared singletons | Per-instance state |
How the trees connect
Lookup starts in the element injector tree, at the component that asks for a dependency. If no ancestor element injector has a provider, Angular continues into the environment injector tree. It begins at the nearest environment injector, such as the one for a route. It then moves up through the root injector to the platform injector.
The first injector that has a matching provider wins. Where you declare a provider therefore decides which instance a component receives.
The same service in each tree
A service with providedIn: 'root' is registered in the root environment injector:
import { Injectable, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class CounterService {
readonly count = signal(0);
increment(): void {
this.count.update(n => n + 1);
}
}Every component that injects CounterService gets the same object. Listing the service in a component's providers adds it to that component's element injector instead:
import { Component, inject } from '@angular/core';
import { CounterService } from './counter.service';
@Component({
selector: 'app-counter',
standalone: true,
providers: [CounterService],
template: `<button (click)="counter.increment()">{{ counter.count() }}</button>`,
})
export class CounterComponent {
protected readonly counter = inject(CounterService);
}The component's own injector is checked first, so it finds this provider before it reaches the root one. Each CounterComponent instance gets its own CounterService, and the root instance is never consulted.
Reading the result
To predict which instance a component gets, ask two questions in order:
- Does this component, or any ancestor component or directive in the DOM tree, list the token in
providers? - If not, does the nearest environment injector, then the root, then the platform, provide it?
The first "yes" gives the instance. If both answers are no, Angular throws NullInjectorError.
Exercise
A service is declared providedIn: 'root'. A parent component lists it in providers. A child component of that parent injects it. Which instance does the child receive, and why?
Show answer
The child receives the parent's instance. Lookup starts in the element injector tree, and the parent's element injector has a matching provider. Angular finds it before it falls back to the root environment injector.