Relationship between Injector & Provider in Angular

1 minute read

A Dependency Provider configures an Injector with a DI Token, which that injector uses to provide the concrete, runtime version of a dependency value. The injector relies on the provider configuration to create instances of the dependencies that it injects into components, directives, pipes, and other services.

Let’s try to understand this relationship with help of real world example. Suppose a customer goes to walmart to buy a computer table (C-Table), here Customer is nothing but a Component and Walmart where customer is searching for C-Table (DI-Token) is an Injector. Now if Computer Table is not available at walmart then it will ask manufacturing industry (Provider) to provide that Computer table.

This example is more precisely explained in video 0008-15- angular: Injector and Provider Relationship 1/4. Snapshot below:

In simple words, Injector has key-value pair and If value is not available for any key (DI token) in injector then it will ask provider to provide the value for that key. So basically provider knows how to create or get that value like Computer Table in above example, here Walmart is Injector and warehouse is Provider.

An Injector & provider has one-to-one relationship, Injector uses the provider. Whenever any component or module asks for any service then an injector associated with that particular component or module will go to provider to get the required service.

Comments