角度 JS 指令卸载事件或等效项

Angular JS Directive Unloading Event or Equivalent

本文关键字:事件 JS 指令 卸载 角度      更新时间:2023-09-26

所以,我有这些小部件:

<widget ng-repeat="widget in widgets"></widget>

如您所知,它们是由 ng-repeat .

因此,当有人删除小部件时,指令中是否有任何地方可以捕获发生的事件或等效物?

.directive('widget', function widget() {
    var directive = { 
        restrict: 'E',
        compile: compile
    };
    return directive;
    function compile() {
        return {
            pre: preLink,
            post: postLink
        };
    }
    function preLink(scope, element) {
    }
    function postLink(scope, element) {
    }
});

您可以收听将在范围销毁之前立即触发的$destroy事件。

$destroy() 通常由 ngRepeat 等指令用于管理循环的展开。

scope.$on('$destroy', function () {
    console.log('captured $destroy event');
});