Angular JS指令卸载事件或等效

Cal*_*ton 5 javascript angularjs

所以,我有这些小部件:

<widget ng-repeat="widget in widgets"></widget>
Run Code Online (Sandbox Code Playgroud)

正如您所知,它们是由创建和删除的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) {

    }
});
Run Code Online (Sandbox Code Playgroud)

Ole*_*leg 9

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

$ destroy()通常由诸如ngRepeat之类的指令用于管理循环的展开.

scope.$on('$destroy', function () {
    console.log('captured $destroy event');
});
Run Code Online (Sandbox Code Playgroud)