Why does Angular read $scope more than needed?

后端 未结 2 964
南笙
南笙 2021-01-27 06:59

I noticed that Angular is less-than-conservative when it comes to reading $scope properties. When the app/controller first instantiate, each bound property defined in the model

相关标签:
2条回答
  • 2021-01-27 07:23

    Try catch stack by firebug but it seem (as I know the angular) that it's from $watch implementation - it must firstly evaluate current hash of object to start "listen" to it's changes. So first reading of object is internal checking and second is for view. Not sure, but it have to read objects twice to implement $watch magic.

    0 讨论(0)
  • 2021-01-27 07:38

    Angular handles two-way binding. The way it does it is through dirty checking. It checks any watched property at the top of the digest and then at the bottom of the digest (twice). It then compares the values to see if anything changed. This is the way it knows whether it needs to rebind the UI. Read this article for reference.

    The digest cycle runs anytime there is an $apply called on the scope. Angular does this often (inside its own directives, e.g. ng-click).

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