I am getting error at following line of the constructor code
this.data = this.getRisks(); .
The error is
Type 'void' is not assignable to type 'any[]'.
Could somebody tell me how do i assign it .
import { Component, OnInit } from '@angular/core';
import { Risk } from './risk';
import { RiskService } from './risk.service';
import { GridModule, GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid';
@Component({
moduleId: module.id,
selector: 'rm-risks',
templateUrl: '/app/risk-list.component.html',
providers: [RiskService]
})
export class RiskListComponent implements OnInit {
private gridView: GridDataResult;
private data: any[];
private pageSize: number = 50;
private skip: number = 0;
title = 'Risk List';
risks: Risk[];
constructor(private _riskService: RiskService) {
this.data = this.getRisks();
this.loadRisks();
}
protected pageChange(event: PageChangeEvent): void {
this.skip = event.skip;
this.loadRisks();
}
private loadRisks(): void {
this.gridView = {
data: this.data.slice(this.skip, this.skip + this.pageSize),
total: this.data.length
};
}
getRisks(): void {
this._riskService.getRisks().then(risks => this.risks = risks);
}
ngOnInit(): void {
this.getRisks();
}
};
risk.service.ts
import { Injectable } from '@angular/core';
import { Risk } from './risk';
import { Risks } from './mock-risk';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';
@Injectable()
export class RiskService {
getRisks(): Promise<Risk[]> {
return Promise.resolve(Risks);
}
}
Since getRisks()
doesn't return anything, simply call it and it will set this.data
constructor(private _riskService: RiskService) {
this.getRisks();
this.loadRisks();
}
You can set this.data = []
if you need it to be defined before data is available.
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