'Inject root dependency from feature loaded module
For an Angular PWA offline capabilities I've created a HttpInterceptor (resolving a Service) that handles web requests when no connection is available.
Right now the service has it's own logic but feature modules are able to register their own handlers using an InjectionToken InjectionToken<IOfflineHandler[]>('OFFLINE_HANDLER').
Sure every core module registering handlers (using .forRoot()) is resolved by my services'
constructor(
@Optional() @Inject(OFFLINE_HANDLER) private _offlineHandlers: IOfflineHandler[]
) { ... }
but since feature modules are only setting providers for their own Injector, I cannot modify them. Right now I'm using the modules' constructor e.g.
constructor(
offlineHandlerService: OfflineHandlerService
) {
offlineHandlerService.registerHandler(new MyOfflineHandler());
}
but that feels somehow like a DI abuse. Is there a pre defined method for achieving this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
