Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS Directive Unloading Event or Equivalent

So, I have these widgets:

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

As you can tell, they are created and removed by the ng-repeat.

So when someone does remove a widget, is there any where in the directive I can catch the event happening OR equivalent?

.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) {

    }
});
like image 904
Callum Linington Avatar asked Jan 19 '15 11:01

Callum Linington


1 Answers

You can listen to the $destroy event which will be fired immediately prior to scope destruction.

The $destroy() is usually used by directives such as ngRepeat for managing the unrolling of the loop.

scope.$on('$destroy', function () {
    console.log('captured $destroy event');
});
like image 197
Oleg Avatar answered Oct 21 '22 06:10

Oleg