Reputation: 21
I have an attribute directive attached to an element in one of my app's templates. When I route away from this template, the controller associated with it receives a destroy event. However, it appears that the directive does not also receive this event. Is there any way that I can receive it within the directive?
Template:
<div ng-controller="myController">
<div my-directive></div>
</div>
In the controller:
$scope.$on('destroy', function(){
console.log('This fires just fine');
})
In the directive's link function:
scope.$on('destroy', function(){
console.log('This will not fire');
})
element.on('destroy', function(){
console.log('This will not fire either');
})
I found an issue from a few years ago that closely resembles my issue, but it was marked as resolved: https://github.com/angular/angular.js/issues/683.
Upvotes: 1
Views: 915
Reputation: 21
Both element.on('$destroy)
and scope.$on('$destroy')
will be called in a directive when the parent element is destroyed. The problem was listening for 'destroy'
instead of '$destroy'
.
Upvotes: 1