Sub topic 7
Scoping services to a route
This chapter assumes you know how standalone components, the Routes configuration, and inject() work. It also assumes you have seen providedIn: 'root' services. The goal is to give a service a lifetime tied to part of the route tree instead of the whole application.
Why scope to a route
A providedIn: 'root' service is created once and shared everywhere. Sometimes that is too broad. An admin area may hold settings that other parts of the app should never see, or a feature may need state that is reset when the user leaves it. Route-level providers handle both cases.
Declaring providers on a route
Add a providers array to a route definition. Angular creates an environment injector for that route, and the providers go into it.
import { Routes } from '@angular/router';
import { AdminComponent } from './admin.component';
import { AdminSettingsService } from './admin-settings.service';
export const routes: Routes = [
{
path: 'admin',
component: AdminComponent,
providers: [AdminSettingsService],
},
];Every component rendered under the admin route shares one AdminSettingsService instance. That includes AdminComponent and any child routes. Components outside this route cannot see it. If one of them injects the service, Angular looks in its own injectors and the root, finds nothing, and throws NullInjectorError.
The service itself needs no providedIn. Leaving it off keeps it from being registered at the root by accident:
import { Injectable, signal } from '@angular/core';
@Injectable()
export class AdminSettingsService {
readonly theme = signal('light');
}Where the route injector sits
The route injector is an environment injector. Its parent is the environment injector of the parent route, or the root injector for a top-level route. Lookups from components under the route pass through these injectors after the element injectors have been checked.
Lifetime of the instance
Angular creates the route injector the first time the route is activated. The injector then stays alive, even after the user navigates away. Returning to /admin reuses the same AdminSettingsService and its state.
This surprises many developers. A route provider is not "one instance per visit". If you need fresh state on every visit, reset it explicitly, or provide the service in the component's providers, which ties it to the component instance.
Choosing between scopes
| Scope | Declared in | Instances | Lives until |
|---|---|---|---|
| Root | `providedIn: 'root'` | One per application | Application closes |
| Route | Route `providers` | One per route injector | Application closes |
| Component | Component `providers` | One per component instance | Component is destroyed |
Common mistakes
- Providing the same service on a route and in
providedIn: 'root'. Components under the route get the route copy, and everything else gets the root copy. Two instances then exist, and state appears to split. - Injecting a route-scoped service from a component outside the route, which causes
NullInjectorError. - Expecting the route instance to be destroyed on navigation.
Exercise
A service is listed in the providers of the admin route and is not marked providedIn: 'root'. A user visits /admin, changes a setting, goes to /home, then returns to /admin. Is the setting still changed? Can a component on /home inject the service?
Show answer
Yes, the setting is still changed, because the route injector is created on first activation and kept, so the same instance is reused. No, a component on /home cannot inject it, because the service is not in any injector on its path, so Angular throws NullInjectorError.