Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the Multi-provider pattern in Angular?
It is a way to provide multiple values or services under the same token, allowing Angular to inject an array of these values wherever the token is used.
Click to reveal answer
beginner
How do you declare multiple providers for the same token in Angular?
Use the multi: true option in the provider object to tell Angular to collect all providers under the same token into an array.
Click to reveal answer
intermediate
Why would you use the Multi-provider pattern?
To combine several services or values that share a role, like multiple logging services or configuration objects, so they can be injected together and used as a group.
Click to reveal answer
beginner
What will Angular inject if multiple providers use the same token with multi: true?
Angular injects an array containing all the provided values or services registered with that token.
Click to reveal answer
beginner
Show a simple example of a multi-provider declaration in Angular.
A. useClass cannot be used with multi-provider pattern
B. multi: true cannot be mixed with providers without it
C. Missing multi: true on the first provider causes only ServiceA to be injected
D. TOKEN must be a string, not a variable
Solution
Step 1: Check multi flag consistency
All providers for a multi-provider token must have multi: true. Missing it on one disables multi-provider behavior.
Step 2: Analyze given providers
The first provider lacks multi: 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 C
Quick 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
A. Register multiple logging services under one token with multi: true, then inject all to run logs
B. Replace existing services with new ones using the same token without multi: true
C. Create a new module that imports all services separately
D. Use multi-provider pattern to inject only the first registered service
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 with multi: 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 A
Quick 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