I've been trying to follow one example in the ng-book 2 and ran into difficulties in the DI chapter.
@Component({
selector: 'app-root',
template: `
<button (click)="invokeService()">Get Value</button>
`
})
export class AppComponent {
constructor(public myService: MyService) {}
invokeService() {
console.log(this.myService.getValue());
}
}
@Injectable()
export class MyService {
getValue(): string {
return 'a value';
}
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [MyService],
bootstrap: [AppComponent]
})
export class AppModule { }
here's a plunker https://plnkr.co/edit/KSXE9tnRj4tffiISyp0i it totally works when creating the ReflectiveInjector manually, but when i try to take the easy way and declare it in providers on the NgModule it fails giving me "Can't resolve all dependencies for AppComponent". I checked the other answers and as you see there are no circular deps or barrel-related problems - it's all in one file. Any hints would appreciated!
If you are putting multiple classes in one file (I'm sure this is for testing only), the order of the files must be in check. Say your
AppComponent
is referencing yourMyService
, so the definition ofMyService
must be put on top ofAppComponent
. Hence, your code must look something like this: