我们是否需要解除绑定事件侦听器的指令时,角开始摧毁? [英] Do we need to unbind event listeners in directives when angular starts to destroy?

查看:174
本文介绍了我们是否需要解除绑定事件侦听器的指令时,角开始摧毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在我的应用程序沉重的内存泄漏,但我还没有发现的原因,这里是背景。

There is a heavy memory leak in my application but I haven't found out the causes, and here is the background.


  • 我使用AngularJS + JQuery的(插件)

  • 很多听众也必然像下面这样:

  • I am using AngularJS + JQuery(plugins)
  • Many listeners are bound like the following:

$(元素)。在(KEYUP功能(){});

$(element).on("keyup", function() {});

所以,问题是

我是否需要按照解除绑定指令中的那些听众?

Do I need to unbind those listeners in directives by following?

scope.$on("$destroy", function() {
    $(element).off();
}); 

顺便说一句,你通常如何找出一个Web应用程序的内存泄漏?
我使用Chrome的个人资料(见这里剖析内存性能
但我无法跟踪到codeS,其中内存泄漏。你有什么建议吗?

BTW, how do you usually find out the memory leak in a web application? I use chrome's profile (see here Profiling memory performance) but I could not trace to the codes where memory leaks. Do you have any suggestions?

非常感谢!

推荐答案

有关范围的角文档销毁,意味着你需要删除DOM事件。

The Angular documentation for scope destroy, implies that you do need remove DOM events.

<一个href=\"http://docs.angularjs.org/api/ng.%24rootScope.Scope#%24destroy\">http://docs.angularjs.org/api/ng.$rootScope.Scope#$destroy

请注意的是,在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.

这篇关于我们是否需要解除绑定事件侦听器的指令时,角开始摧毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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