I have an AngularJS directive and I need to perform certain actions if the directive's element is removed from the DOM (either from inside an AngularJS call or by any other method, like jQuery).
Is that possible?
I have an AngularJS directive and I need to perform certain actions if the directive's element is removed from the DOM (either from inside an AngularJS call or by any other method, like jQuery).
Is that possible?
Share Improve this question asked Jun 11, 2015 at 10:22 alexandernstalexandernst 15.1k25 gold badges107 silver badges213 bronze badges2 Answers
Reset to default 8In the directive, when an element is removed from DOM, $destroy event is emitted. Inside your directive's link function, you can do this:-
element.on('$destroy', function() {
// do stuff
});
For more information and plete example, see documentation here
EDIT: See this plunker to see $destroy in action. Here, I am removing the element after 2 seconds, and logging destroyed in console.
When your directive is removed from the DOM, an $destroy
event is fired. See here https://docs.angularjs/api/ng/type/$rootScope.Scope#$destroy
In this question (Provide an example of scope's $destroy event?) I found the following example:
ctrl.directive('handleDestroy', function() {
return function(scope, tElement, attributes) {
scope.$on('$destroy', function() {
alert("In destroy of:" + scope.todo.text);
});
};
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745314034a4622132.html
评论列表(0条)