What is DI Token

1 minute read

When we think of learning Dependency Injection in Angular, couple of questions arises like

  • How this DI concept works in Angular?
  • What are all the important components that make it work?

This video 0008-12- angular: Dependency Injection (DI) Token part 1/3 will help you to get answers for most of your queries. It makes you understand

  • What that core component is?
  • Why it is required?
  • What exactly it does?

This component is known as DI Token (Dependency Injection Token) which is nothing but a key part in key-value pair similar to a lookup key which does lookup in lookup table.

E.g. If there is class A needs class B as dependency, so in order to know where that B instance exist and to get that & give it to Class A, angular need some identifier that name or key to find/identify the instance of B is known DI Token.

DI Token should be valid at run time (JavaScript) and compile time (Typescript) both. A token can be a String, a Class or a Type. Refer to video to understand various token types in detail with some real time examples. Snapshot below:

So in simple words we can define DI Token as a key to lookup for instance of the dependencies and it has to be valid at run time.

Comments