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;
}
1

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.