1

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)

David
  • 589
  • 6
  • 21

1 Answers1

0

What you are looking for here is a basic Singleton, if I'm not mistaking?

See the following topic on how to create one

rguerin
  • 2,068
  • 14
  • 29
  • no not a singleton, I need either the parent instance or a new instance depending on a condition that's multiple possible instances – David Oct 10 '18 at 15:19