Angular 2: Inject Service to another service. (No provider error)

A providers array is available in @NgModule. You can provide your service by declaring your service in that array.

@NgModule({
    declarations:[...],
    imports:[...],
    providers: [MyCustomService],
    bootstrap:[AppComponent]
})

You need to provide your service somewhere. Please refer to angular2 docs

You could provide it in the bootstrap method:

bootstrap(AppComponent,[TodoStore,Dispatcher]);

or the app component:

@Component({
    ...
      providers:[TodoStore,Dispatcher]
}
...

Or in any other component, depending on your needs.

Also, you don't need to @Inject(Dispatcher) in the constructor. It's basically the same as

constructor(dispacher:Dispatcher){
}

Oh yeah, welcome to SO :)


Thank for the reply.

Since it is not a Component, the @Component(...) solution does not apply.

bootstrap(AppComponent,[TodoStore,Dispatcher]); 

solution works. But that makes this main.ts a central place.

Tags:

Angular