I have two services in my app - MainService and RightClickService. Only MainService is accessible globally in the application, and RightClickService is injected to MainService. Therefore, I defined the following files as:
app.module.ts
@NgModule({ declarations: [ AppComponent, ... ], imports: [ BrowserModule, FormsModule, HttpModule ], providers: [MainService], bootstrap: [AppComponent] }) export class AppModule { } RightClickService is not mentioned in app.module.ts.
service.ts
@Injectable() export class MainService { constructor(private rightClickService: RightClickService) { console.log("Constructor is init"); } } RightClickService exists only in one component named inside the big application RightClickComponent.
right-click.component.ts:
@Component({ selector: 'right-click', template: ` ... `, styleUrls: ['...'], providers: [RightClickService] }) export class RightClickComponent implements OnInit { constructor(private rightClickService: RightClickService) {} } Still, I get the error:
EXCEPTION: No provider for RightClickService! Do you know what I'm doing wrong?
(private rightClickService : MainService)MainServicecan't find provider forRightClickServicebecause you don't provide it in NgModuleMainServicewill be created before, you have a race condition here, you cannot use theMainServicebecause it depends of yourRightClickService.