Sub topic 11
How Angular resolves a dependency
This chapter assumes you know TypeScript classes, standalone components, basic services, and how to configure routes. It explains the order in which Angular searches for a provider, so you can predict which instance a component receives.
Two trees, one search
Angular keeps two injector hierarchies. Element injectors follow the component and directive tree in the DOM. Environment injectors hold providers from providedIn: 'root', route providers, bootstrapApplication, and NgModules.
When code calls inject(Token), Angular searches in two phases:
- It checks the element injector of the requesting component or directive. If that injector has no provider, it checks each parent element injector, up to the root component's host element.
- If no element injector has the token, it moves to the environment injector hierarchy. It starts with the nearest environment injector, which is the route injector if the component was created by a route. It then walks up through parent route injectors to the root environment injector and finally the platform injector.
If no injector provides the token, Angular throws NullInjectorError, unless you passed optional: true.
Following a lookup
Consider a CounterService provided in three places: providedIn: 'root' on the service, providers: [CounterService] on ParentComponent, and nowhere else. A ChildComponent inside ParentComponent's template calls inject(CounterService).
Angular checks ChildComponent's own injector and finds nothing. It moves to the parent element injector, which belongs to ParentComponent. That injector has a provider, so the search stops. The child gets the instance that ParentComponent created. The root instance is never consulted.
A component placed elsewhere, outside ParentComponent, gets the root instance. Two instances of CounterService therefore exist at the same time, and which one you get depends on where you sit in the tree.
Because element injectors are searched first, a component-level provider always wins over a root or route provider for the same token.
Seeing the result
This code makes the lookup visible. Each service instance gets an id when it is created.
import { Component, Injectable, inject } from '@angular/core';
let nextId = 1;
@Injectable({ providedIn: 'root' })
export class IdService {
readonly id = nextId++;
}
@Component({
selector: 'app-leaf',
standalone: true,
template: `<p>Leaf sees instance {{ ids.id }}</p>`,
})
export class LeafComponent {
protected readonly ids = inject(IdService);
}
@Component({
selector: 'app-scoped',
standalone: true,
imports: [LeafComponent],
providers: [IdService],
template: `<app-leaf />`,
})
export class ScopedComponent {}A LeafComponent rendered inside ScopedComponent reports the instance created by ScopedComponent's providers. A LeafComponent rendered anywhere else reports the root instance, which has a different id.
Common mistakes
- Expecting the template's parent to decide. The search follows the element tree where the component is declared in a template. A component created by a route starts from the router outlet's position, then continues to the route's environment injector.
- Assuming route providers override component providers. They do not. Element injectors are always searched first.
- Providing the same service at two levels by accident. The result is two instances and state that does not sync. Check whether
providedIn: 'root'and aprovidersarray both name the service.
Exercise
A service has providedIn: 'root'. PageComponent lists it in providers. PageComponent's child, RowComponent, injects it. Which instance does RowComponent get, and why?
Show answer
It gets the instance created by PageComponent. Angular searches RowComponent's element injector first, then its parent's, which is PageComponent's. That injector has the provider, so the search ends before reaching the root environment injector.