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?
You can inject an Angular service in a component, service, directive etc by specifying the service and its type in a component's constructor. Note that injecting a service through a class constructor is, in general, tree-shakable.
Angular Creates the Module Injector tree when the Application starts. At the top of the Module Injector tree, Angular creates an instance of Null Injector . The Null Injector always throws an error unless we decorate the dependency with the Optional decorator.
When you add a service provider to root module (root injector), it is available for whole application. That means if you have a feature module with service in providers and that service is also provided in root module, in this case both modules will work with the same instance of service (singleton pattern).
In addition to what already said, you must know two things:
Your app has only one root injector that contains all providers delcared in @NgModule.providers
array of any module in your app including AppModule
.
Each Component has its own injector (child injector of the root injector) that contains providers declared in @Component.providers
array of the component.
when angular want to resolve dependencies (RightClickService) of a service (MainService) it looks for it in the root injector which contains providers of all NgModules.
When angular want to resolve dependencies (RightClickService) of a component (RightClickComponent) it looks for it in the component injector if not found it looks for it in the parent component injector if not found he will do the same until he reaches the root injector if not found an error will be thrown.
if you want to solve the problem you can do this :
function MainServiceFactory(RightClickService) {
return new MainService(new RightClickService());
}
@NgModule({
declarations: [
AppComponent,
...
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [
{
provide: MainService,
useFactory: MainServiceFactory,
deps: [RightClickService]
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
I just pulled this into a Plunker, and unless I mistyped something ... what you have seems to work. So maybe there is something else wrong?
Can you check out the plunker here: https://plnkr.co/edit/ea9dFs?p=info
(The tool won't let me post without the code ... but you already have the code above ... so I'm just pasting a piece of it.)
import { Injectable } from '@angular/core'
@Injectable()
export class RightClickService {
constructor() {
console.log("RightClickService is init");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With