2017-02-10 7 views
1

アングル2アーク機能を使用してレンダリングするだけですか、何か間違っていますか?アングル2は矢印機能を使用してのみレンダリングします

this.service.getData(o).subscribe(res => { 
    this.data = res.data 
    this.view = res.view 
}); 

実は私のコンポーネントをレンダリングしますが、

this.service.getData(o).subscribe(function(res){ 
    this.data = res.data 
    this.view = res.view 
}); 

エラーではありませんが、あなたはcontextを失っているので、私のコンポーネントは

答えて

1

を更新していません。

let self = this; 

this.service.getData(o).subscribe(function(res){ 
    self.data = res.data 
    self.view = res.view 
}); 

取る購読observerオブジェクトです。あなたのコードの意味でthisobserverオブジェクトのコンテキスト。

またはこのメソッド使用:

this.service.getData(o).subscribe((function(res){ 
    this.data = res.data 
    this.view = res.view 
}).bind(this)); 
関連する問題