检测是否从DOM中删除了指令 [英] Detect if directive was removed from DOM
问题描述
可以这样做?
在该指令中,当一个元素从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屋!