我需要QUOT&;取消监视"当范围被销毁范围变量? [英] Do I need to "unwatch" scope variables when the scope is being destroyed?

查看:123
本文介绍了我需要QUOT&;取消监视"当范围被销毁范围变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是棱角分明的文档。我看几个变量是此范围内建立了NG-网过滤字符串的一部分。当此范围被破坏,我需要通过调用$范围。$腕表的返回值来停止监视他们,或者是范围足够的破坏,处理呢?如果变量被监视是不是这个范围之内?

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 destroyed, do I NEED to unwatch them by calling the return value from $scope.$watch, or is the destruction of the scope enough to deal with that? What if the variables being watched were NOT part of this scope?

我将导致内存泄漏/会导致性能问题,如果我不正在使用的范围一起被毁坏了停止监视的变量。

Will I leak memory/cause performance problems if I don't "unwatch" variables that are being destroyed along with the scope.

删除从父范围当前范围(及其所有的孩子)。去除意味着到$电话消化()将不再传播到目前的范围及其子女。拆除也意味着,目前的范围是符合垃圾收集。

Removes the current scope (and all of its children) from the parent scope. Removal implies that calls to $digest() will no longer propagate to the current scope and its children. Removal also implies that the current scope is eligible for garbage collection.

的$销毁()通常用于由指令如ngRepeat用于管理环的展开

The $destroy() is usually used by directives such as ngRepeat for managing the unrolling of the loop.

一个范围被破坏就在一个$破坏事件在此范围内播出。应用code可以注册一个$破坏事件处理程序,将会给它执行任何必要的清理工作的机会。

Just before a scope is destroyed, a $destroy event is broadcasted on this scope. Application code can register a $destroy event handler that will give it a chance to perform any necessary cleanup.

请注意的是,在AngularJS,也有一个$破坏jQuery的事件,该事件可以前一个元素从DOM除去用来清理DOM绑定

Note that, in AngularJS, there is also a $destroy jQuery event, which can be used to clean up DOM bindings before an element is removed from the DOM.

推荐答案

一个快速浏览一下源$ C ​​$ C会告诉你,到$表返回的去除功能并不做任何事情的异域风情。它只是从范围中删除的项目。$$观察家阵列。

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.

这篇关于我需要QUOT&;取消监视"当范围被销毁范围变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆