Full answer
Normally, when two providers use the same token, the last one wins. With multi: true, each provider adds one entry to an array, and the injector returns all entries together.
Pattern:
- Create a token for one item:
new InjectionToken<MenuItem>('MENU_ITEM'). - Different features add providers:
{ provide: MENU_ITEM, useValue: {...}, multi: true }. - A consumer injects
inject(MENU_ITEM)and receivesMenuItem[]. If nobody provided one, it fails, so give a default factory that returns an empty array, or use{ optional: true }.
Typical uses:
- Plugins and feature registration: each feature library registers its menu entry, route links, or search provider, without the shell knowing them.
- Validation rules, formatters, or exporters that can be extended.
- Angular itself:
NG_VALIDATORS,NG_ASYNC_VALIDATORS,NG_VALUE_ACCESSOR,APP_INITIALIZER, and (in the classic API)HTTP_INTERCEPTORS.
Details that matter:
- Order: entries are in the order the providers were registered, so that merging configs with
mergeApplicationConfigcan change the order. Do not depend on order unless you sort, for example with apriorityfield. - All providers for the same token must use
multi: true. Mixing a multi and a non-multi provider for one token causes an error or surprising results. - The scope matters. Providers at different injector levels do not merge: a child injector's multi providers replace the parent's array for that subtree, not add to it.
- Wrap registration in a helper function that returns
ProviderorEnvironmentProviders, such asprovideMenuItem(item), so callers do not repeatmulti: true.
Compared with a service that has a register() method, multi providers need no start-up ordering: all contributions exist when the injector is created.
typescript
import { InjectionToken, Provider, inject } from '@angular/core';
export interface MenuItem {
label: string;
path: string;
priority: number;
}
export const MENU_ITEMS = new InjectionToken<MenuItem[]>('MENU_ITEMS', {
factory: () => [],
});
export function provideMenuItem(item: MenuItem): Provider {
return { provide: MENU_ITEMS, useValue: item, multi: true };
}
export function sortedMenu(): MenuItem[] {
return [...inject(MENU_ITEMS)].sort((a, b) => a.priority - b.priority);
}
// In a feature: providers: [provideMenuItem({ label: 'Marks', path: '/marks', priority: 2 })]
// In another: providers: [provideMenuItem({ label: 'Home', path: '/', priority: 1 })]Angular 20.3, type-checked. A Karma test checks that two multi providers give an array in registration order and that the sorted helper orders it by priority.
Key points
- `multi: true` collects provider values into an array under one token.
- Use it for plugin points, and wrap it in a `provide...` helper.
- All providers for the token must be multi. Order follows registration. Child injectors do not merge with parents.
- Angular uses it for validators, value accessors and initialisers.
Common mistakes
- Forgetting `multi: true` on one provider, so it replaces the others.
- Depending on the order of the array.
- Expecting a child injector to add to the parent's array.
Follow-ups
- How would you add a priority to plugin entries?
- What does a token with a `factory` default give you?
- Why is `NG_VALUE_ACCESSOR` a multi provider?
Tips
- Use the menu items example. It is clear and realistic.
- Say that child injectors replace, not extend.
- Mention the `provide...` helper function style.
Evaluation rubric
- Explains multi providers and the resulting array.
- Gives a plugin example and Angular's own uses.
- Knows the scope and order caveats.