Sub topic 8
Element injectors follow the component tree
This chapter assumes you know TypeScript classes and decorators, standalone components, and how to inject a basic service. It covers the second of the two injector hierarchies: element injectors.
What an element injector is
Every element in a component template that hosts a component or directive can have an element injector. Angular creates the injector only when something needs it, which means a component or directive with a providers array, or a descendant that injects from it. The element injectors form a tree that mirrors the DOM structure of your templates, not the class inheritance of your components.
When a component asks for a dependency, Angular checks its own element injector first, then walks up through the parent elements. If no element injector has the provider, the lookup moves to the environment injectors (route, root, platform). This chapter focuses on the first part of that walk.
Providing at the element level
You add a provider to an element injector with the providers array of @Component or @Directive. This component gets its own CounterService:
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);
}Place two <app-counter /> elements on a page. Each creates its own CounterService, so clicking one button does not change the other. If CounterService is also providedIn: 'root', the root instance still exists, but these components never reach it. The element injector answers first.
Children share the nearest provider
A descendant does not need its own providers entry. It receives the instance from the closest ancestor element injector that provides the token. If CounterComponent contains a child component that injects CounterService, both share one instance. Two sibling CounterComponent elements do not share, because neither is an ancestor of the other.
| Where the provider is declared | Who shares the instance |
|---|---|
| Root (`providedIn: 'root'`) | The whole application |
| `providers` on a component | That component, its template children and its projected content |
| `providers` on two sibling components | Nothing. Each gets its own instance |
Lifetime follows the element
An instance created by an element injector lives as long as the element. When Angular destroys the component, for example when @if removes it, the instance is discarded. If the service implements ngOnDestroy, Angular calls it. A new element later gets a fresh instance with fresh state.
Directives work the same way
A directive's providers array registers on the element that carries the directive. Every component and directive on that element, and everything below it, can inject the provider. This lets you attach scoped behavior to any element without writing a wrapper component.
Exercise
A page shows <app-counter /> twice. CounterService is listed in CounterComponent's providers and is also providedIn: 'root'. You click the first button three times. What does the second button show, and which instance does each component use?
Show answer
The second button still shows 0. Each CounterComponent has its own element injector with its own CounterService, so the two instances are independent. The root instance is never used by either, because the element injector answers first.