All topics
Servicesadvanced

Multi-providers

Explain the multi: true provider flag and how it lets multiple providers contribute to a single token.

Normally, registering a second provider for the same token simply replaces the first one — the last provider registered for a given token wins. multi: true changes this behavior for a specific token: instead of replacing, each provider registered with multi: true against that token contributes an additional entry to an array, and injecting the token returns the whole array rather than a single value.

A normal provider is like a name tag with one slot — write a new name and the old one is gone; a multi-provider is like a guestbook where every new entry is added to a growing list, and reading the guestbook shows everyone who's signed in, not just the most recent visitor.

Key Concepts

1
This is precisely the mechanism behind HTTP_INTERCEPTORS: Angular itself defines that token as a multi-provider, so every interceptor you register doesn't overwrite the previous one — it adds to a growing list that HttpClient then chains together at request time. Understanding this connects the Multi-providers topic directly back to HTTP Interceptors, and interviewers like asking about HTTP_INTERCEPTORS specifically as a concrete, checkable example of multi: true in the wild.
HTTP_INTERCEPTORSHttpClientmulti: true
2
Beyond Angular's own built-in tokens, multi: true is genuinely useful application code any time you want an extensibility point where multiple, independently-registered contributors should all participate — a plugin system where several feature modules each register their own validators, a collection of app-wide error handlers that should all run, or a set of route guards contributed from different feature areas.
multi: true
3
An interview-relevant gotcha: forgetting multi: true when you intend to add a second provider for an already-multi token (or vice versa, adding it when you actually wanted a plain override) produces very confusing runtime behavior — either your second interceptor silently vanishes, or injecting what you expected to be a single value unexpectedly comes back as an array.
multi: true