Sub topic 6
Application-wide singletons with providedIn root
This chapter assumes you know TypeScript classes and decorators, standalone components, and how to inject a basic service. It covers the most common way to share one service instance across a whole Angular application.
What providedIn root does
Writing providedIn: 'root' in @Injectable registers the service in the root environment injector. This is the injector Angular creates when the application bootstraps. Every component, directive, pipe and service asks this injector, directly or through its ancestors, so they all receive the same 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);
}
}No providers array is needed anywhere. The decorator is the registration.
Sharing one instance
Two unrelated components can inject the service. Both read and write the same signal.
import { Component, inject } from '@angular/core';
import { CounterService } from './counter.service';
@Component({
selector: 'app-counter-button',
standalone: true,
template: `<button (click)="counter.increment()">Add</button>`,
})
export class CounterButtonComponent {
protected readonly counter = inject(CounterService);
}
@Component({
selector: 'app-counter-display',
standalone: true,
template: `<p>Count: {{ counter.count() }}</p>`,
})
export class CounterDisplayComponent {
protected readonly counter = inject(CounterService);
}Clicking the button in one component updates the display in the other, because inject(CounterService) returns the same object in both places.
Why it is tree-shakable
The provider information lives on the service class itself. If nothing in your application ever injects CounterService, the build tool can drop it from the bundle. Listing a service in an NgModule or in bootstrapApplication providers creates a reference that keeps it in the bundle even when unused.
The instance is also created lazily. Angular constructs it the first time something requests it, not at startup.
Root providers for tokens
Services are not the only values you can place in the root injector. An InjectionToken can declare a default factory. Providers passed to bootstrapApplication then override that default.
import { InjectionToken } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
export const API_URL = new InjectionToken<string>('API_URL', {
providedIn: 'root',
factory: () => '/api',
});
bootstrapApplication(AppComponent, {
providers: [{ provide: API_URL, useValue: 'https://example.com/api' }],
});Code that injects API_URL receives https://example.com/api. In a test that does not override it, the code receives /api.
Common mistakes
- Providing the service again lower down. If a component lists
CounterServicein itsproviders, that component and its children get a new instance. The root instance is bypassed, so state is no longer shared. - Expecting a lazy route to get its own copy. A service with
providedIn: 'root'stays a single instance even when it is first injected inside a lazy-loaded route. Lazy loading does not create a new root. - Storing per-user or per-view state in a root service. The instance lives as long as the application. State left in it carries over between views, so reset it or scope the service more narrowly.
Exercise
A component declares providers: [CounterService] while CounterService is also marked providedIn: 'root'. Does that component share its count with the rest of the application? Why or why not?
Show answer
No. The component's own element injector finds CounterService first and creates a new instance for that component and its children. The root instance is never reached, so the counts are separate.