I have a component that depends on a service that may either be provided by its parent component or create a new instance of that service depending on a dynamic condition.
The problem I have is that if I specify a provider, or even a service provider, I lose the parent instance of the service. Is it possible to get a handle on the parent instance?
providers: [ myserviceProvider]
pseudo code in myServiceProvider:
provide: MyService,
useFactory: myServiceFactory,
deps: [AuthorisationService]
const myServiceFactory = (authorisationService: AuthorisationService) => {
return authorisationService.isAuthorised() ? /** what goes here? *// : new MyService();
};
In the /* what goes here? */ space I want to return the existing instance of myservice which would be injected if I hadn't declared a provider
edited for clarity: the parent instance wouldn't be the root instance of the service (it has its own provider as well which is just injecting using standard provider syntax)