sax
sax

Reputation: 838

Angular: remove providers injected from module in the component providers declaration

I have declaration of providers in app module which provides list of implementations of MyProviderService class (I am using multi: true). Now I would like to "undefine" this list in one component completely, so I added:

@Component({
  providers: [
    { provide: MyProviderService, useValue: undefined },
  ],
})

That works and injector has empty value for the MyProviderService. I am wondering if there is a way to remove the MyProviderService from injector completely. Something that would later require to mark the service as @Optional in the constructor for DI.

Upvotes: 0

Views: 978

Answers (1)

Andrei
Andrei

Reputation: 12021

No, there is no possibility to nullify one of the provided things. Injectors refer to the parent injector, if some token is not found in their scope, otherwise it is found and returned back to the querying code.

However you can spawn a component with a newly created injector that has no link to parent injector. In this case there will be no any items available in the corresponding DI subtree, and there will be not possible to inject other items as well.

Upvotes: 1

Related Questions