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

后端 未结 2 1330
忘掉有多难
忘掉有多难 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:25

    I also think that this should be explicitely explained in the documentation. I've deduced that unwatching was unnecessary from the angular source code which always ignores the result of scope.$watch (in ngIf, ngShow, ngRepeat, etc.).

    0 讨论(0)
  • 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.

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