Sub topic 12
Multi Providers
This chapter assumes you already know how to create an InjectionToken, how to register providers with useClass, useValue, and useFactory, and how the injector hierarchy decides which provider wins.
One token, many values
A normal provider maps one token to one value. If you register a second provider for the same token, it replaces the first. That works for "give me the logger." It does not work for "give me every search source," where several features each want to add their own entry.
A multi provider changes this. When you add multi: true to a provider, Angular collects every provider registered for that token into an array. Consumers inject the token and receive the whole array.
A working example
Each search source below is registered separately. The component knows only the token, so it never needs to know which sources exist.
import { Component, Injectable, InjectionToken, inject } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
export interface SearchSource {
name: string;
search(term: string): string[];
}
export const SEARCH_SOURCES = new InjectionToken<SearchSource[]>('SEARCH_SOURCES');
@Injectable()
export class DocsSource implements SearchSource {
name = 'docs';
search(term: string): string[] {
return [`Guide: ${term}`];
}
}
@Injectable()
export class ProductsSource implements SearchSource {
name = 'products';
search(term: string): string[] {
return [`Product: ${term} Pro`];
}
}
@Component({
selector: 'app-root',
standalone: true,
template: `
@for (result of results; track result) {
<p>{{ result }}</p>
}
`
})
export class AppComponent {
private readonly sources = inject(SEARCH_SOURCES);
protected readonly results = this.sources.flatMap(s => s.search('angular'));
}
bootstrapApplication(AppComponent, {
providers: [
{ provide: SEARCH_SOURCES, useClass: DocsSource, multi: true },
{ provide: SEARCH_SOURCES, useClass: ProductsSource, multi: true },
{
provide: SEARCH_SOURCES,
useValue: { name: 'faq', search: (t: string) => [`FAQ: ${t}`] },
multi: true
}
]
});The page shows three lines, in registration order. Every provider recipe works with multi: true, so you can mix useClass, useValue, useFactory, and useExisting entries under one token.
Where Angular uses this
The framework uses the same pattern for extension points:
| Token | Purpose |
|---|---|
| `HTTP_INTERCEPTORS` | Class-based HTTP interceptors, run in registration order |
| `ENVIRONMENT_INITIALIZER` | Functions that run when an environment injector is created |
| `NG_VALIDATORS` | Validator directives contributed to a form control |
| `APP_INITIALIZER` | Startup work, now usually registered via `provideAppInitializer()` |
Many provideX() helper functions return multi providers internally. That lets you call several of them without one overwriting another.
Common errors
- Expecting a merge across injectors. Multi providers are collected per injector. If a component lists
{ provide: SEARCH_SOURCES, ..., multi: true }in its ownproviders, consumers under that component get only that component's array. The root entries are not appended. The nearest injector that has the token still wins, as with any other token. - Relying on order you don't control. The array follows registration order. Order matters for interceptors and initializers. Keep related registrations together, or sort by an explicit
priorityfield. - Typing the token wrong. Declare the token as
InjectionToken<SearchSource[]>, notInjectionToken<SearchSource>. Otherwiseinject()returns a type that doesn't match the array you actually receive.
Exercise
The root providers register two SEARCHSOURCES entries with multi: true. A child component adds `providers: [{ provide: SEARCHSOURCES, useClass: LocalSource, multi: true }]`. How many sources does a component inside that child receive, and why?
Show answer
One, LocalSource. Each injector collects its own multi providers. Resolution stops at the nearest injector that provides the token, which is the child's element injector, so the root's two entries are not included.
Exercise
You add { provide: SEARCH_SOURCES, useClass: DocsSource } next to an existing { provide: SEARCH_SOURCES, useClass: ProductsSource, multi: true }. What happens?
Show answer
Angular throws an error, because regular providers and multi providers cannot be mixed for the same token. Add multi: true to the DocsSource provider as well.