且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测指令是否已从 DOM 中删除

更新时间:2023-11-30 19:57:40

在指令中,当一个元素从 DOM 中移除时,会发出 $destroy 事件.在指令的链接函数中,您可以执行以下操作:-

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

请参阅这个 plunker 以了解 $destroy 的作用.在这里,我在 2 秒后删除元素,并在控制台中销毁日志记录.

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