Angular Dependency Injection(Custom Provider)
Tags: angular, typescript, javascript, webdev
Published: 2021-06-19 08:19:36

## Angular Dependency Injection

Before discussing dependency injection lets understand what are dependencies first

- Dependencies are services or objects that the class needs to perform its function
- Dependency Injection is a design pattern in which a class/ component request dependencies from external sources rather than creating them.

In Angular, Component or services are injected with dependencies via the constructor arguments.  Angular DI's framework will provide dependencies calling the factory function to instantiate the dependencies. 

How Services are exposed to Inject?

Its using @Injectable decorator and dependency injection provider which is declared in service.

![Untitled](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ympjgdcjd6zyx22z5s4v.png)
 

In the above service named AngProvidedDepService the if provided In is removed then you will get following error

![Untitled 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mppdw8pjmbis8bridnbj.png)
 
No provider for "service_name". This is because Angular not able to find any provider which will provide AngProvidedDepService. 

## Lets create our own Custom Provider

- Provider is simply a function which needs to be passed to Angular dependency Injection system. System will call this function and this function will provide the dependency its needed

In our case we have to return AngProvideDepService which needs to provide its own dependencies also as shown below inside the function.

![Untitled 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03kjbw7yd0nmge2f3710.png) 

We created a Custom service provider which will return new AngProvidedDepService. Here comes one more question how will you provide dependencies to the provider that is also through Angular Dependency Injection system.

Next step is to plug this provider into Angular Dependency Injection system. There are multiple ways doing that lets provide to component inside Component decorator

We will use providers property and pass it an configuration object. In this Configuration object we will telling exactly what we are providing using a unique name to service via Injection Token.

Lets create one Unique Injection token for Angular Provided service and then pass it to the providers property available in Component annotation

![Untitled 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rovvbuamc97f4dxvkeh8.png)
 

Lets provide Configuration object to provider

![Untitled 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3n9qarfbt8tl8z391tz.png)
 

- provide will have unique token for the service i.e. Injection token which is created
- use factory will be provided with custom provider created
- deps will have the dependencies which is needed for AngDepProvidedService to run.

We have provided all these to Angular dependency Injection system so that whenever any component use this via constructor Service will be provided

Lets inject it in one of the compoent using inject decorator

![Untitled 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmvaylvvmp0s767lv4f3.png) 

Summarize :

Angular Dependency Injection system will provide services or objects which were injected into components via constructor. Each Dependency has its own Injection token and provider function which will provide the instantiation. To go deeper into we have created our custom provider and Custom injection token and updated into one of the component.

Thanks for Reading...

If you liked this reading follow me on this for technical blogs

[Abi Aradhya - Medium](https://abiaradhya.medium.com/)

[abi - DEV Community Profile](https://dev.to/abilashs003)

[Abilash S - Software Developer - Siemens Healthineers | LinkedIn](https://www.linkedin.com/in/abilash-s-aa305997/)

follow me on insta for personal profile 

[](https://www.instagram.com/abi_aradhya/)

For Subscribing my next upcoming articles click here

[CodeWithAbi](https://codewithabi.hashnode.dev/)