I need to have some Angular services configured dynamically, depending on a runtime switch. In days before AOT, I got it to work using the following code:
@N
I found one way to achieve it: Expose the configuration via a provider, then have injected to a "static" factory function. The code above would look like this:
// Necessary if MyConfiguration is an interface
export const MY_CONFIG = new OpaqueToken('my.config');
// Static factory function
export function someOtherServiceFactory(config: MyConfiguration,some: SomeService, http: Http) {
switch (config.type) {
case 'cloud':
return new SomeOtherService(new SomethingSpecificForCloud());
case 'server':
return new SomeOtherService(new SomethingSpecificForServer());
}
}
@NgModule({
imports: [HttpModule],
providers: []
})
export class MyModule {
static forRoot(config: MyConfiguration): ModuleWithProviders {
return {
ngModule: MyModule,
providers: [
SomeService,
{ provide: MY_CONFIG, useValue: config },
{
provide: SomeOtherService,
useFactory: someOtherServiceFactory,
deps: [MY_CONFIG, SomeService, Http]
},
]
};
}
}
It works and all, but I would still be very interested in knowing whether this is actually a good idea, or if I'm doing something terribly wrong and should be taking a completely different approach to solving this problem.
I found another solution:
More information and sample project at my blog.