2016-05-24 8 views
1

私は2の単純なモデルを持っています。私は毎秒html.getが実行されますが、ビューに配置された変数はそれ自体をリフレッシュしません。何が問題ですか ?Angular2間隔1のhttp.getはビューを更新しません

constructor(private _cm: Communication) {} 

ngOnInit() { 
    setInterval(() => { 
     this.getDeviceData(); 
     this.numCountChange.next(this.responseDate) 
    }, 1000); 
} 

ngOnDestroy() { 
    clearInterval(this.requestInterval); 
} 

getDeviceData() { 
    this._cm.devicesService.getAccessPointStateByIdDeviceAndIdAccessPoint(10018, 10154) 
      .subscribe(
       accessPoint => { 
        this.device0 = accessPoint; 
        this.device0.stateChecker(); 
        this.responseDate = new Date() 
        console.log(this.responseDate); 
       }, 
       error => console.log(error) 
      ); 
} 

HTMLは:

    < div class="small-box-footer"> 
         {{ responseDate }} 
        < /div> 

responseDateは、すべての時間を示して最初の応答値

答えて

1
それは手動で変更検出を起動せずに動作するはずですが、私は実際にやっている例 devicesService.getAccessPointStateByIdDeviceAndIdAccessPointのためかわからない

コードの外観

これは、その周りに、少なくとも動作するはずです:

constructor(private _cm: Communication, private cdRef:ChangeDetectorRef) {} 

ngOnInit() { 
    setInterval(() => { 
     this.getDeviceData(); 
     this.numCountChange.next(this.responseDate) 
    }, 1000); 
} 

ngOnDestroy() { 
    clearInterval(this.requestInterval); 
} 

getDeviceData() { 
    this._cm.devicesService.getAccessPointStateByIdDeviceAndIdAccessPoint(10018, 10154) 
      .subscribe(
       accessPoint => { 
        this.device0 = accessPoint; 
        this.device0.stateChecker(); 
        this.responseDate = new Date() 
        console.log(this.responseDate); 
        this.cdRef.detectChanges() 
       }, 
       error => console.log(error) 
      ); 
} 
+0

このソリューションは動作しますが、私は別のエラーを表示するためのリンクが EXCEPTION発生し得るとき:破壊されたビューを使用しようとしていますトリッキーな例外ザッツdetectChanges – Emerceen

+0

を。 'this.subscription = this._cm.deviceService.getAccessPoint ... subscribe()'や 'ngOnDestroy() 'のようなクラスプロパティ(例えば' subscription')に 'subscribe'の戻り値を格納してみてください。 'this.subscription.unsubscribe()'を呼び出しますか? –

関連する問題