Sub topic 10
Inspecting dependency injection
This chapter assumes you can read Angular components and services, know what inject() and constructor injection do, and have Angular DevTools installed on an app running in development mode. You should also be comfortable selecting a component in the component explorer and reading its properties.
Why inspect injectors
Dependency injection (DI) bugs rarely show up as clear errors in the template. Instead you get a service that holds stale data, a second instance of a service you expected to be shared, or a NullInjectorError for a provider you thought was registered. Angular DevTools lets you see which injectors exist for a selected component and what each one provides. That turns "where does this value come from?" into something you can check rather than guess.
Opening the injector view
- Open the Components tab in Angular DevTools.
- Select a component or directive in the tree.
- In the right-hand panel, find the injector section. In current versions this is reached through the "View injector tree" control, or the injector view shown beside the properties panel. The exact label varies between DevTools releases.
The view shows the chain of injectors Angular walks when it resolves a dependency for the selected node. Resolution starts at the node's own element injector and moves outward. It continues through parent element injectors, and then through the environment injectors.
The injector hierarchy
Angular has two parallel hierarchies. Both are searched when a dependency is requested.
| Hierarchy | Where providers come from | Typical scope |
|---|---|---|
| Element injector | `providers` and `viewProviders` on a component or directive, plus `providers` on routes with component-level scope | One component subtree |
| Environment injector | `providedIn: 'root'`, `bootstrapApplication` providers, and route-level `providers` | Application or a lazy-loaded route |
The lookup stops at the first injector that has a provider for the token. If it reaches the end, the null injector throws. DevTools shows the same ordering, so you can see which level supplies the value.
A worked example
Suppose a counter service is meant to be shared across the whole app, but each widget seems to have its own count.
import { Component, Injectable, inject, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class CounterService {
count = signal(0);
}
@Component({
selector: 'app-widget',
standalone: true,
providers: [CounterService], // creates a new instance for each widget
template: `<button (click)="add()">{{ counter.count() }}</button>`,
})
export class WidgetComponent {
counter = inject(CounterService);
add() {
this.counter.count.update((n) => n + 1);
}
}The service is declared as providedIn: 'root', so you would expect one shared instance. The component's own providers array registers another provider. Because the element injector is searched first, each WidgetComponent gets its own CounterService.
To confirm this in DevTools:
- Select one
app-widgetin the component tree. - Open its injector view and find
CounterServicelisted under the component's element injector. - Select a second widget and check the same entry. Each widget has its own provider entry, while the root environment injector also lists the service.
The fix is to remove providers: [CounterService] from the component. Reload, select the widget again, and confirm that CounterService now appears only under the root environment injector.
Common errors
- Provider in the wrong scope. A service in a component's
providersis recreated with each component instance, and it is destroyed with it. Move it torootor a route'sprovidersif it should be shared. viewProvidersversusproviders.viewProvidersare not visible to projected content. A projected child can fail to find a service that the host's template children can find.- Lazy routes with their own providers. A route that declares
providerscreates its own environment injector. Services provided there are separate from root instances, even with the same class. - Missing provider. A
NullInjectorErrormeans no injector in the chain provides the token. DevTools can help you see which levels were searched.
Exercise
A service is marked providedIn: 'root', but two sibling components each show a different value from it. The service's code has no bug. What do you check in DevTools, and what is the likely cause?
Show answer
Select each sibling and open its injector view. Check whether the service appears under each component's element injector. The likely cause is a providers: [TheService] entry on the component (or a shared parent route's providers), which creates a separate instance for each. Removing the component-level provider makes both use the single root instance.