How Angular Change Detection is triggered when you are binding to a function?

前端 未结 1 1226
悲哀的现实
悲哀的现实 2021-01-05 15:25

From these two posts:

  • The mechanics of DOM updates in Angular
  • Angular 2 Performance: Is it better to bind with a data member than a function?
相关标签:
1条回答
  • 2021-01-05 16:03

    Angular doesn't know or care about the content of the function.

    Angular will call func() every time change detection runs and compare if the previous result is the same as the current result.

    Because calling a function and comparing the result is much more expensive than just comparing the previous value with the current value, it's better to use an event to update a property with the function result and bind the view only to the property, instead of to a function directly.

    If the function returns different values on subsequent calls (with the same parameter values) you'll get an exception in development mode like

    the model has changed since it was last checked

    0 讨论(0)
提交回复
热议问题