2016-07-24 8 views
0

NgOnInit関数を使用してサーバーからデータを取得する際に問題が発生しています。このデータで、私はmorris.jsを使ってプロットをペイントしたいが、私はそのデータを取得していない。Angular2 OnInit、サーバーからデータを取得しない

私はDBからすべての車の賠償額を取得し、その後、全額の賠償額を毎日プロットしたいと考えています。私は賠償の空配列を取得していると私は何をペイントすることはできません。

マイコード:

constructor(_router: Router, public http: Http, private _reparacionsService: ReparacioService) { 
    this.router = _router; 
} 

getReparacions() { 
    this._reparacionsService.getReparacions().subscribe(rep => this.reparacions = rep.json()); 
} 

ngOnInit() { 
    this.getReparacions(); 
    this.getLastWeek(); 
    this.getReparacionsDia(); 
    window['Morris'].Line({ 
     element: 'repDaychart', 
     data: this.reparacionsDies, 
     xkey: 'data', 
     ykeys: ['totalReparacions'], 
     labels: ['totalReparacions'], 
     parseTime: false, 
     xLabels: "day" 
    }); 
} 

私はすでにインポートとのOnInit宣言:

import {Component, OnInit} from 'angular2/core'; 
    export class DashBoard implements OnInit 

私が使用している角度ベータ17 感謝。

答えて

1

このコードは

this._reparacionsService.getReparacions().subscribe(rep => this.reparacions = rep.json()); 

非同期(async)で、これは

ngOnInit() { 
    this.getReparacions(); 
    this.getLastWeek(); 

this.getLastWeek();this.reparacions = rep.json()前に実行されることを意味し

を実行しているあなたは、おそらく

getReparacions() { 
    return this._reparacionsService.getReparacions().map(rep => this.reparacions = rep.json()); 
} 

ngOnInit() { 
    this.getReparacions().subscribe(data => { 
    this.getLastWeek(); 
    this.getReparacionsDia(); 
    window['Morris'].Line({ 
     element: 'repDaychart', 
     data: this.reparacionsDies, 
     xkey: 'data', 
     ykeys: ['totalReparacions'], 
     labels: ['totalReparacions'], 
     parseTime: false, 
     xLabels: "day" 
    }); 
    }); 
} 
のような何かをしたいです

他のコール(getLastWeek()、getReparacionsDia() `)も非同期でない場合にのみ動作します。

+0

私はそれを試してみましたが、私はエラーを取得しています: はTypeError:プロパティを読み取ることができません未定義の「購読する」プロパティを読み取ることができません。DashBoard.ngOnInitで 未定義の「購読する」(dashboard.component.ts:36) でAppView._View_DashBoard_Host0.detectChangesInternal(DashBoard_Host.template.js:34)AppView.detectChangesで (angular2.dev.js:23638)AppView.detectContentChildrenChangesで (angular2.dev.js:23657)AppView._View_Home0.detectChangesInternalで ( Home.template.js:1338) –

+0

申し訳ありません、 'getReparacions() 'に' return'を追加するのを忘れました。 –

+0

ありがとう!今はエラーは出ませんが、プロットを描画しません。私はそれを探します。 –

関連する問題