检测是否从DOM中删除了指令 [英] Detect if directive was removed from DOM

查看:141
本文介绍了检测是否从DOM中删除了指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AngularJS指令,如果指令的元素从DOM中移除(从AngularJS调用或任何其他方法,如jQuery),我需要执行某些操作。



可以这样做?

解决方案

在该指令中,当一个元素从DOM中删除时,$ destroy事件被排出。在你的指令的链接功能里面,你可以这样做: -

  element.on('$ destroy',function() b $ b // do stuff 
});

有关更多信息和完整示例,请参阅文档 here



编辑:参见这个plunker 看到$ destroy在行动中。在这里,我在2秒钟后删除元素,日志在控制台中销毁。


I have an AngularJS directive and I need to perform certain actions if the directive's element is removed from the DOM (either from inside an AngularJS call or by any other method, like jQuery).

Is that possible?

解决方案

In the directive, when an element is removed from DOM, $destroy event is emitted. Inside your directive's link function, you can do this:-

element.on('$destroy', function() {
   // do stuff
});

For more information and complete example, see documentation here

EDIT: See this plunker to see $destroy in action. Here, I am removing the element after 2 seconds, and logging destroyed in console.

这篇关于检测是否从DOM中删除了指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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