Service not getting injected correctly and page shows no results + angular 2
This code fetches the risks, but doesn't do anything with it
getRisks(): void { this._riskService.getRisks();}
I assume it should be
getRisks(): void { this.risks = this._riskService.getRisks();}
and I assume ngOnInit()
should be
ngOnInit(): void { // this._riskService.getRisks(); this.getRisks();}
You have to teach the injector how to make a Risk Service
. You do this by registering a RiskService
provider. You have to do this inapp.component.ts
.
In your component under the template add provider [RiskService]
Here is the explanatioin from the angualr 2 site.
You have to include your service into the NgModule
import { NgModule } from '@angular/core';import { BrowserModule } from '@angular/platform-browser';import { AppComponent } from './app.component';import { RiskListComponent } from './components/risks/risk-list.component';import { RiskService } from './risk.service';@NgModule({ imports: [BrowserModule], declarations: [AppComponent, RiskListComponent], bootstrap: [AppComponent], providers: [RiskService]})export class AppModule { }
The more logic implementation for me in your code, is use that service as singleton. So you don't need to include it on the Component declaration as a provider. Only in the constructor.