Reputation: 4678
I have a service called CalculationService, which is declared @Injectable()
. This service uses a whole lot of other injected classes, each of which is also declared as @Injectable()
and given to the constructor of CalculationService via @Inject()
.
Now when I want to use the service inside my app, the AppComponent looks like this (for the sake of convenience, I omitted the Typescript imports):
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
RouterModule.forRoot(routeConfig),
PlannerModule
],
providers: [
InputService,
Bausparen,
BausparenInput,
Girokonto,
GirokontoInput,
Immobilien,
ImmobilienInput,
Lebensversicherung,
LebensversicherungInput,
Rentenversicherung,
RentenversicherungInput,
Tagesgeld,
TagesgeldInput,
Termingeld,
TermingeldInput,
Wertpapiere,
WertpapiereInput
],
bootstrap: [AppComponent]
})
export class AppModule {
}
I don't need to access all the providers declared in the app module directly. The only class in need is the CalculationService but I don't know how to declare the providers in the Service.
Is there a way to refactor the service as a module, i.e. declare the providers inside the CalculationService? Or do I have to get rid of all the @Injectable()
s (Bausparen and so on) and create objects inside the Calculation Service, so that no provider is needed?
Upvotes: 5
Views: 3551
Reputation: 657731
You can't register providers on or in services. You can only register them in modules or at components.
What you can do is to create a module for your service, then you only need to add the module to imports: [...]
and all providers are registered globally.
If it is a lazy loaded module you need to implement and import forRoot()
Upvotes: 3