angular: Providers does not work when setting them via bootstrapModule


i was trying to specify provider when bootstrapping module like :

var apieProvider = new ApiProvider(); platformBrowserDynamic().bootstrapModule(SomeModule, { providers: [provide(ApiProvider , { useValue: apiProvider })] });

Having :

@NgModule({ imports: [BrowserModule], bootstrap: [SomeComponent] }) export class SomeModule{ }

and get exception that ‘No provider ApiProvider found’

but if i mimic provider setup in NgModule by writing it straight to metadata everything works fine:

((<any>Reflect).getMetadata('annotations', SomeModule)[0]) .providers.push(provide(ApiProvider , { useValue: this.apieProvider }));

  • Angular version: 2.0.0-rc.5
  • Browser: [all ]
  • Language: [TypeScript 1.8]

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 24 (9 by maintainers)

Most upvoted comments

Here is how i did it without using window:

export const AppModule = (settings: ServerSettings) => {
        imports: [
        declarations: [AppComponent],
        providers: [
                provide: ServerSettings,
                useValue: settings,
        bootstrap: [AppComponent]

    class AppModule { }

    return AppModule;


// ... Some code to get the settings

then it’s injectable anywhere

export class AppConfig {
    constructor(private serverSettings: ServerSettings) {
        console.log("Server Settings", this.serverSettings);

Passing the providers to platformBrowserDynamic was what eventually worked for me:

platformBrowserDynamic([{ provide: CONFIG, useValue: config }])
  .catch((error) => console.error(error))

Is this still working for you in latest version(s) of angular? Services I’m providing in platformBrowserDynamic are not being made acaiable to the rest of the app.

@vytautas-pranskunas- being able to provide providers dynamically would break offline compilation and so it is not supported. A set of providers needs to be know statically.

Dynamically one can bind a provider to a factory and then one has a freedom to create any value for that provider. This should be sufficient.

Having the ability to dynamically add a provider (which was not present during static analysis) is strange as each provider needs both the configuration, and site where it is injected (the user). So adding one without the other is not useful.

Closing as this is working as intended.

Those providers are used to bootstrap the compiler, which compiles all your components. They’re not part of the application injector. For that, you need to specify the providers in your app module.

I have created plunker: Hope did not missed anything, but an idea is clear. I have updated plunker and got same error