Callum Linington
Callum Linington

Reputation: 14417

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

    }
});

Upvotes: 5

Views: 1839

Answers (1)

Oleg
Oleg

Reputation: 9359

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');
});

Upvotes: 9

Related Questions