2017-03-06 7 views
1

これらの2行のコードの違いは何ですか(2行目に "setThing"を呼び出すインライン関数があることを除いて)。 最初のケースで角度変化検出が実行されなかったことに気付きました。角型のパラメータで関数を渡す方法の違い

someObservable<Thing>.subscribe<Thing>(this.setThing) // change detection didn't run 
someObservable<Thing>.subscribe<Thing>(thing => this.setThing(thing)); // change detection worked 

setThing(thing :Thing) { 
    this.thing = thing; 
} 
+2

[どのように "この" キーワードwork?](http://stackoverflow.com/questions/3127429/how-does-の可能性のある重複この - キーワード - 仕事) – toskv

答えて

3

機能内にthisを使用しているとします。最初のものでは、字句thisを失いますが、2番目にはありません。

例:

まず1:

setThing(thing :Thing) { 
    this.myTemplateThing = thing; // Since your this is not refering to the component you won't be seeing a change 
} 
関連する問題