Sub topic 9
One service instance per component with providers
This chapter assumes you know TypeScript classes and decorators, standalone components, and how to write a basic injectable service. It also assumes you have seen inject() and route configuration.
The problem: a shared singleton is not always what you want
A service marked providedIn: 'root' lives in the root environment injector. The whole application shares one instance.
import { Injectable, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class CounterService {
readonly count = signal(0);
increment(): void {
this.count.update(n => n + 1);
}
}Suppose a page shows three counters, and each should count on its own. With this service, all three would read and write the same signal. You need one CounterService per component instance.
The fix: list the service in `providers`
The providers array on a component creates an entry in that component's element injector. Angular builds one element injector for each component instance in the DOM tree. When the component asks for the service, Angular finds the entry at the component itself and creates a new service there.
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);
}Each <app-counter> now gets its own CounterService. The providedIn: 'root' setting on the class is still there, but the lookup never reaches the root injector because the element injector answers first.
Lifetime of a scoped instance
The service is created lazily, the first time something injects it in that scope. It is destroyed with the component. If the service implements ngOnDestroy, Angular calls it when the component is destroyed. This makes component-level providers a good place for state and cleanup that should not outlive a view.
What children see
Child components and directives in the template can also inject the service. They walk up the element injector tree and find the instance provided by the nearest ancestor that lists it. So a parent component can create a service and share it with its descendants, without sharing it with the rest of the app.
Two components that sit side by side each get their own instance, because neither is an ancestor of the other.
Rules of thumb
- Use
providerson a component when the service holds state that belongs to one view, such as form state, a selection, or a timer. - Use
providedIn: 'root'when the data is truly application-wide. - A directive can also list
providers. The instance is then scoped to the element that carries the directive.
Exercise
A page contains two <app-counter> elements, and CounterComponent lists CounterService in its providers. The service is also providedIn: 'root'. How many CounterService instances exist once both counters have rendered, and why?
Show answer
Two. Each CounterComponent instance gets its own CounterService from its element injector, which Angular checks before the root environment injector. The root instance is never created, because nothing in the page asks the root injector for it.