I couldn't figure out the right way to do it, but here is what I want to achieve:
abstract class Engine
class TurboEngine extends Engine
class RocketEngine extends Engine
I have an angular application which is injecting 'Engine' in a lot of places and using it's methods to fulfill the business needs.
My only problem is that I have to inject TurboEngine and RocketEngine at the same time on every places and with a condition, decide which to call.
This is what I want to change. Since I have an abstract Engine, I'd like to inject it ONCE in my main component, and then having injected automatically by that way in my components.
So basically defining the provider for a service at runtime.
Here is what I've tried, but didn't work:
var injector = ReflectiveInjector.resolveAndCreate([
provide(Engine, {useClass: TurboEngine})
]);
I did it in my main component where it worked, but all other places I've got the following error:
No provider for TurboEngine! (Engine -> TurboEngine)
Updated:
The way I want to use it:
I have component
A
which injects Engine (that's where I want to choose an implementation, so either TurboEngine or RocketEngine based on some conditions.Then I have component
B
, which is inside componentA
's template. Here when I'm trying to injectEngine
I would expect to have the same instantiated object as I manually created in componentA