The multi-provider pattern lets you register many services under the same token. Angular then gives you all of them as a list. This helps when you want to add features without changing existing code.
Multi-provider pattern in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Angular
providers: [
{ provide: TOKEN, useClass: ServiceA, multi: true },
{ provide: TOKEN, useClass: ServiceB, multi: true }
]Set multi: true to tell Angular this token has multiple providers.
Angular injects an array of all providers registered with that token.
Examples
Angular
providers: [
{ provide: 'LOGGERS', useClass: ConsoleLogger, multi: true },
{ provide: 'LOGGERS', useClass: FileLogger, multi: true }
]Angular
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true }
]Sample Program
This example shows two greeter services registered under the same token GREETERS using multi-provider. The component injects all greeters as an array and displays their greetings.
Angular
import { Component, Inject, Injectable, InjectionToken } from '@angular/core'; import { CommonModule } from '@angular/common'; const GREETERS = new InjectionToken<{greet(): string}>('GREETERS'); @Injectable() class EnglishGreeter { greet() { return 'Hello'; } } @Injectable() class SpanishGreeter { greet() { return 'Hola'; } } @Component({ selector: 'app-root', standalone: true, imports: [CommonModule], template: `<div *ngFor="let greeter of greeters"> {{ greeter.greet() }} </div>`, providers: [ { provide: GREETERS, useClass: EnglishGreeter, multi: true }, { provide: GREETERS, useClass: SpanishGreeter, multi: true } ] }) export class AppComponent { constructor(@Inject(GREETERS) public greeters: { greet(): string }[]) {} }
Important Notes
Without multi: true, Angular would overwrite previous providers for the same token.
Use multi-providers to keep code modular and easily extendable.
Summary
Multi-provider pattern lets you register many providers under one token.
Angular injects an array of all providers registered with that token.
This pattern helps add features without changing existing code.
Practice
1. What does the Angular multi-provider pattern allow you to do?
easy
Solution
Step 1: Understand the multi-provider pattern concept
The multi-provider pattern allows registering many providers under one token so Angular can inject all of them as an array.Step 2: Compare options with the concept
Only Register multiple providers under a single injection token correctly describes this ability. Other options describe unrelated Angular features.Final Answer:
Register multiple providers under a single injection token -> Option DQuick Check:
Multi-provider pattern = Register many providers under one token [OK]
Hint: Multi-provider means many providers for one token [OK]
Common Mistakes:
- Confusing multi-provider with multiple components
- Thinking it relates to modules instead of providers
- Assuming it limits injection to once
2. Which syntax correctly registers multiple providers using the multi-provider pattern in Angular?
easy
Solution
Step 1: Identify correct multi-provider syntax
To register multiple providers under one token, each provider must havemulti: trueset.Step 2: Check options for correct usage
Only providers: [{ provide: TOKEN, useClass: ServiceA, multi: true }, { provide: TOKEN, useClass: ServiceB, multi: true }] setsmulti: truefor both providers. providers: [{ provide: TOKEN, useClass: ServiceA }, { provide: TOKEN, useClass: ServiceB }] missesmulti, options withmulti: falseor invalid strings like'yes'do not work.Final Answer:
providers: [{ provide: TOKEN, useClass: ServiceA, multi: true }, { provide: TOKEN, useClass: ServiceB, multi: true }] -> Option BQuick Check:
multi: true enables multi-provider registration [OK]
Hint: Always set multi: true for multi-provider registration [OK]
Common Mistakes:
- Omitting multi: true causes only last provider to register
- Setting multi: false disables multi-provider behavior
- Using string instead of boolean for multi
3. Given this Angular provider setup:
What will Angular inject when you ask for
providers: [
{ provide: 'TOKEN', useValue: 'A', multi: true },
{ provide: 'TOKEN', useValue: 'B', multi: true }
]What will Angular inject when you ask for
inject('TOKEN')?medium
Solution
Step 1: Understand multi-provider injection behavior
When multiple providers use the same token withmulti: true, Angular injects an array of all values in registration order.Step 2: Analyze given providers
Providers register 'A' first, then 'B'. So injection returns ['A', 'B'].Final Answer:
An array ['A', 'B'] -> Option AQuick Check:
Multi-provider injects array of all registered values [OK]
Hint: Multi-provider injects array in registration order [OK]
Common Mistakes:
- Assuming only last provider is injected
- Thinking order is reversed
- Expecting a single value instead of array
4. What is wrong with this Angular multi-provider registration?
providers: [
{ provide: TOKEN, useClass: ServiceA },
{ provide: TOKEN, useClass: ServiceB, multi: true }
]medium
Solution
Step 1: Check multi flag consistency
All providers for a multi-provider token must havemulti: true. Missing it on one disables multi-provider behavior.Step 2: Analyze given providers
The first provider lacksmulti: true, so Angular treats it as a single provider, overriding others. Only ServiceB with multi: true is ignored.Final Answer:
Missing multi: true on the first provider causes only ServiceA to be injected -> Option CQuick Check:
All multi providers must have multi: true [OK]
Hint: All multi providers need multi: true or injection breaks [OK]
Common Mistakes:
- Mixing multi: true and missing multi flags
- Thinking useClass is incompatible with multi
- Assuming token must be string literal
5. You want to add logging features to an Angular app without changing existing services. How can the multi-provider pattern help?
hard
Solution
Step 1: Understand the goal
You want to add logging features without changing existing services, so you need to add providers without replacing them.Step 2: Apply multi-provider pattern
Register multiple logging services under one token withmulti: true. Angular injects all as an array, allowing combined logging without modifying existing code.Final Answer:
Register multiple logging services under one token with multi: true, then inject all to run logs -> Option AQuick Check:
Multi-provider adds features by injecting arrays of providers [OK]
Hint: Use multi: true to add features without replacing services [OK]
Common Mistakes:
- Replacing instead of adding providers
- Not using multi: true and losing existing providers
- Confusing multi-provider with module imports
