javascript - Detect if directive was removed from DOM - Stack Overflow

I have an AngularJS directive and I need to perform certain actions if the directive's element is

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 badges
Add a ment  | 

2 Answers 2

Reset to default 8

In 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

相关推荐

  • javascript - Detect if directive was removed from DOM - Stack Overflow

    I have an AngularJS directive and I need to perform certain actions if the directive's element is

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信