I've got a module composed with a Component and a bunch of sub component. There is also a service dedicated to this module.
I am not sure to understand the injection mecanism for my needs. What's i want to do is to inject the Service into all sub component.
When i inject Service with a provider into components it's ok but i wanted to set provider on module level.
Module
import { NgModule } from '@angular/core';
import { ChangeUserComponent } from './change-user.component';
import { ChangeUserSidebarComponent } from './sidebar/sidebar.component';
import { ChangeUserUserListComponent } from './user-list/user-list.component';
import { ChangeUserService } from './change-user.service';
@NgModule({
declarations: [
ChangeUserComponent,
ChangeUserUserListComponent,
ChangeUserSidebarComponent
],
providers : [
ChangeUserService // <-- when i set that it's not working
],
exports : [
ChangeUserComponent
]
})
export class ChangeUserModule
{
}
Sub component
import { Component, OnDestroy, OnInit } from '@angular/core';
import { ChangeUserService } from '../change-user.service';
@Component({
selector : 'change-user-sidebar',
templateUrl: './sidebar.component.html',
styleUrls : ['./sidebar.component.scss'],
//providers: [ ChangeUserService ] // <-- with that the service is correctly injected
})
export class ChangeUserSidebarComponent
{
public activeNode: any;
constructor(
private _changeUserService: ChangeUserService
)
{
}
}
Service
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class ChangeUserService
{
constructor(
private _http: HttpClient
){}
}
Service is correctly injected when i set Provider on the component level but i wanted to set it on module level.
Edit : When i set keyword provider on Component level : everything is ok
When is set keyword providers on module level :
Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[ChangeUserSidebarComponent -> ChangeUserService]: StaticInjectorError(Platform: core)[ChangeUserSidebarComponent -> ChangeUserService]: NullInjectorError: No provider for ChangeUserService!
Is there a way to do that ? Am I missing something ?