Hopefully, the following helps. Instead of using the global variable directly in any service or component, I found it easier to maintain the third party dependency if we could hook into angular bootstrapping. Ex.
@NgModule({
providers: [{
provide: APP_INITIALIZER,
useFactory: ThirdPartyFactory,
deps: [ThirdPartyService],
multi: true
}]
})
export class AppCommonModule {}
export function ThirdPartyFactory(service: ThirdPartyService) {
return () => service.init();
}
Since the factory uses a promise
, you can code a function inside the ThirdPartyService
that wraps the third party global variable.
This solution takes a bit of time to implement. But the good side is that it's more manageable and portable to another module or application. The reason I mentioned APP_INITIALIZER
is that you can do all the above before the app starts. Otherwise, the service ThirdPartyService
itself will do the trick.