有没有办法检测 AngularJS 中的指令破坏?

Is there a way to detect directive destruction in AngularJS?

我想在 AngularJS 的指令中使用 $timeout。但是我在指令文档中找不到检测它何时被销毁的方法,以防它发生在我的超时结束之前,我需要清理超时。

是否有我可以绑定到的事件或一些内置函数(类似于控制器的 $destroy)我可以用来检测我的指令何时被销毁?还是我缺少有关指令的基本概念?

你提到的$destroy事件也可以用在指令中:

app.directive('myDirective', function() {
  return {
    link: function(scope) {
      scope.$on('$destroy', function() {
        // Clean up
      });
    }
  };     
});