1

What is the difference between these two lines of code (except the fact that second line has an inline function calling "setThing"). I noticed that on the first case Angular change detection didn't run.

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;
}
Karolis
  • 147
  • 6
  • 2
    Possible duplicate of [How does the "this" keyword work?](http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – toskv Mar 06 '17 at 15:29

1 Answers1

3

Assuming you are using this inside the function. In the first one you lose the lexical this, in the second you don't.

Example:

First one:

setThing(thing :Thing) {
    this.myTemplateThing = thing; // Since your this is not refering to the component you won't be seeing a change
}
eko
  • 34,608
  • 9
  • 60
  • 85