Providing services dynamically in Angular

116 Views Asked by At

I have a DI problem I am trying to resolve without bloating my code:

The setup is as follows:

I have a base form class 'A' that communicates with some of its child components employing service 'B'.

Class 'A' has many (say 50) inheriting form components, and if I want to provide the service to each, I would have to go through each component declaration and provide the service 'B'.

So, why not just provide in root you ask? Well, this service needs to live just for the while the form lives, and although it is possible, it will require me to track the life of the form and on deactivate guard clear the service ... Not a very formidable way of solving the problem (so to speak...).

Another thought that crossed my mind was to provide the service ... dynamically ... But I think this is not supported / the way to do things in Angular? Probably this is just not supported.

I could provide a Service Locator pattern, but this will just compete with the existing DI.

0

There are 0 best solutions below