Do I need to “unwatch” scope variables when the scope is being destroyed?

后端 未结 2 1339
忘掉有多难
忘掉有多难 2021-01-11 12:38

Below are the docs from angular. I\'m watching several variables that are part of this scope to build up a filter string for ng-grid. When this scope is being

2条回答
  •  耶瑟儿~
    2021-01-11 13:33

    A quick look at the source code will show you that the removal function returned by $watch doesn't do anything exotic. It simply removes an item from the scope.$$watchers array.

    As such, once the scope is destroyed, the entire array goes with it, and the garbage collector will clean everything up for you.

    Regardless of what you are watching, the watch itself is stored in the scope. Which is why, that in order to use $watch, you don't call angular.$watch, but rather you call $scope.$watch.

提交回复
热议问题