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

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

问题描述

我的应用程序中存在严重的内存泄漏,但我还没有找到原因,这里是背景.

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:

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

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

问题是

我是否需要按照以下步骤在指令中解除绑定这些侦听器?

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

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

顺便说一句,您通常如何发现 Web 应用程序中的内存泄漏?我使用 chrome 的配置文件(请参阅此处分析内存性能)但我无法追踪到内存泄漏的代码.您有什么建议吗?

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?

非常感谢!

推荐答案

范围销毁的 Angular 文档暗示您确实需要删除 DOM 事件.

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

http://docs.angularjs.org/api/ng.$rootScope.Scope#$destroy

注意,在 AngularJS 中,还有一个 $destroy 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.

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

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