Sub topic 10
providers versus viewProviders
This chapter assumes you know how element injectors follow the component tree and how providers on a component creates a per-instance service. It covers the one other place a component can register a provider, and how that changes which children can see it.
Two views of a component
A component has two kinds of children:
- View children are the elements in the component's own template.
- Content children are elements the parent writes between the component's tags. They reach the template through
<ng-content />.
A provider in providers is visible to both kinds. A provider in viewProviders is visible only to view children. Content children skip it and keep searching up the tree.
An example
LabelService is deliberately not providedIn: 'root', so the only way to get it is through a component.
import { Component, Injectable, inject } from '@angular/core';
@Injectable()
export class LabelService {
readonly text = 'provided by the card';
}
@Component({
selector: 'app-card-body',
standalone: true,
template: `body: {{ label.text }}`,
})
export class CardBodyComponent {
protected readonly label = inject(LabelService);
}
@Component({
selector: 'app-note',
standalone: true,
template: `note: {{ label ? label.text : 'no service' }}`,
})
export class NoteComponent {
protected readonly label = inject(LabelService, { optional: true });
}
@Component({
selector: 'app-card',
standalone: true,
imports: [CardBodyComponent],
viewProviders: [LabelService],
template: `<app-card-body /><ng-content />`,
})
export class CardComponent {}
@Component({
selector: 'app-root',
standalone: true,
imports: [CardComponent, NoteComponent],
template: `<app-card><app-note /></app-card>`,
})
export class AppComponent {}CardBodyComponent sits in the card's template, so it finds LabelService and renders "provided by the card". NoteComponent is written inside <app-card> in AppComponent's template, so it is a content child. It cannot see the card's viewProviders, and optional: true makes it receive null. It renders "no service".
If you change viewProviders to providers, both children get the service.
When to use viewProviders
Use viewProviders when a service is an implementation detail of the component's template. Hiding it keeps projected content from depending on it, or from picking it up by accident.
Consider a component that offers a service to its template and also accepts arbitrary projected content. Users may project a component that injects the same token. With providers, that projected component gets your internal instance. With viewProviders, it falls through to whatever an ancestor provides, which is usually what the author of the projected content expects.
Details to remember
viewProvidersexists only on@Component. Directives have no view, so@Directivesupports onlyproviders.- If the same token appears in both, view children get the
viewProvidersentry. - The
hostoption ofinject()stops the lookup at the host element of the current component. AviewProvidersentry on that host is still found, which is whyhostis often paired withviewProviders.
Exercise
A component declares viewProviders: [LabelService] and its template contains <ng-content />. A projected child calls inject(LabelService) without optional, and no ancestor provides LabelService. What happens, and what is the smallest change that makes the injection succeed?
Show answer
Angular throws a NullInjectorError, because the projected child is a content child and cannot see viewProviders. The smallest fix is to move LabelService from viewProviders to providers on the component. Adding it to an ancestor's providers or using providedIn: 'root' would also work.