#Accessing env vars when registering providers
6 messages · Page 1 of 1 (latest)
for those providers that you're registering dynamically, you can also inject any other provider that is available to the module as usual. So yes, you can use ConfigService
can you show us an example on how you're doing the provider registration?
Two options:
- if you want to conditionally include/exclude providers from the
providersarray, your only options is readingprocess.envdirectly (or usingConditionalModule, which is basically the same thing with extra steps). TheConfigServiceis only available via DI.
- If you only want to switch implementations, you can use a factory provider with
moduleRef
providers: [
{
provide: MyProvider,
inject: [ConfigService, ModuleRef],
useFactory: (config, moduleRef) => {
const kind = config.get('kind')
if (kind == 'typeA') {
return moduleRef.create(TypeAProvider)
} else {
return moduleRef.create(TypeBProvider)
}
}
}
]
@fresh zephyr Amazing thank you - the second scenario is exactly what I was aiming for. Much appreciated !