Sub topic 3
Providers versus viewProviders
Assume you know TypeScript classes, Angular components, services, standalone components, and basic routing.
providers registers a dependency for the component and for descendants, including projected content. viewProviders registers a dependency only for the component's own view. Projected content keeps searching outside the component's view injector.
@Component({
selector: 'app-panel',
standalone: true,
template: `<ng-content />`,
providers: [{ provide: Theme, useClass: DarkTheme }],
viewProviders: [{ provide: Layout, useClass: CompactLayout }],
})
export class Panel {}A child declared inside the panel's template can inject Layout. A component passed through <ng-content> cannot use that Layout provider, but it can use Theme.
How Angular resolves a dependency
Angular starts at the requesting element's injector and searches upward through element injectors. If no matching provider exists, it searches the associated environment injector hierarchy, such as a route or the application root. The first matching provider wins. Each injector can create and cache its own instance.
Changing the lookup
Use resolution modifiers when the default search is too broad:
optional: truereturnsnullinstead of throwing when no provider exists.self: truesearches only the current injector.skipSelf: truestarts with the parent injector.host: truestops the search at the current host boundary.
const local = inject(Theme, { self: true });
const parent = inject(Theme, { skipSelf: true, optional: true });
const maybeHost = inject(Theme, { host: true, optional: true });Common pitfalls and debugging
NullInjectorError means Angular found no provider in the allowed search path. Check the token, the provider's scope, and any modifiers. Unexpected duplicate instances usually come from listing a service in component or route providers when providedIn: 'root' was intended.
Testing scoped providers
TestBed providers do not replace providers declared on a component. Override the component definition instead.
TestBed.configureTestingModule({ imports: [Panel] });
TestBed.overrideComponent(Panel, {
set: { providers: [{ provide: Theme, useClass: FakeTheme }] },
});Injecting a parent through an abstract token
A parent can expose itself without making children depend on its concrete class:
providers: [{ provide: PanelApi, useExisting: forwardRef(() => DialogComponent) }]A descendant can then call inject(PanelApi, { optional: true }). Angular resolves the token through the element injector tree.
NgModule injectors and lazy-loaded modules
NgModules can also provide services. A lazy-loaded module receives a child module injector, so its providers are separate from the application's root providers. Modern standalone routing usually expresses the same scope with route-level providers.
Exercise
A projected child cannot inject a service listed only in its parent's viewProviders. Where should the service be registered if the projected child must share it?
Show answer
Register it in the parent's providers, or in an ancestor environment injector.