What is multi True provider in angular?

1 minute read

Multi: true means that one provider token provides an array of elements. Using multi: true we tell Angular that the provider is a multi provider. This is used mostly in those scenarios where we use primitives or chunk of values.

Generally we do not use it in injecting the array of classes, but there are some use cases in framework where we do. Let’s understand with one real world example. Suppose we are using two services pricing service and sales service. In sales we are doing order and in pricing we are charging for that order. Both of them have their own configs and at one place we want to put all configs together, this is the place where we use multi: true. So when we need multiple strings to be passed as a single array, then we use multi: true.

To get more details, please visit our video Multi True Provider | mulTitrue | Angular DI Tutorial

As we mentioned earlier with multi providers, we can provide multiple values for a single token in DI. Usually, when we register multiple providers with the same token the last one wins.
For example all directives for router support routerLink, routerOutlet are provided by ROUTER_DIRECTIVES. If a new provider is registered with the token ROUTER_DIRECTIVES, then it overrides the previously registered directives. If multi: true (on the first registered and the new provider) is set, the new directives are added to the previously registered directives instead of overriding.

Comments