How do I use $scope.$watch and $scope.$apply in AngularJS?

前端 未结 6 2027
栀梦
栀梦 2020-11-21 15:36

I don\'t understand how to use $scope.$watch and $scope.$apply. The official documentation isn\'t helpful.

What I don\'t understand specifi

6条回答
  •  执念已碎
    2020-11-21 16:20

    I found very in-depth videos which cover $watch, $apply, $digest and digest cycles in:

    • AngularJS - Understanding Watcher, $watch, $watchGroup, $watchCollection, ng-change

    • AngularJS - Understanding digest cycle (digest phase or digest process or digest loop)

    • AngularJS Tutorial - Understanding $apply and $digest (in depth)

    Following are a couple of slides used in those videos to explain the concepts (just in case, if the above links are removed/not working).

    In the above image, "$scope.c" is not being watched as it is not used in any of the data bindings (in markup). The other two ($scope.a and $scope.b) will be watched.

    From the above image: Based on the respective browser event, AngularJS captures the event, performs digest cycle (goes through all the watches for changes), execute watch functions and update the DOM. If not browser events, the digest cycle can be manually triggered using $apply or $digest.

    More about $apply and $digest:

提交回复
热议问题