How to destroy a directive?

10,684 views
Skip to first unread message

Tanguy Krotoff

unread,
Jan 13, 2013, 12:01:49 PM1/13/13
to ang...@googlegroups.com
Hi all,

I want to cleanly destroy my directive. I've found two different ways to do that: scope.$on('$destroy'...) and element.bind('$destroy'...)

app.directive('fullcalendar', ...) {
  return {
    restrict: 'A',

    link: function(scope, element) {

      // Solution 1
      scope.$on('$destroy', function() {
        console.log('scope on destroy');
        element.fullCalendar('destroy');
      });

      // Solution 2
      element.bind('$destroy', function() {
        console.log('element bind destroy');
        element.fullCalendar('destroy');
      });

      element.fullCalendar({
        ...
      });
    }
  };
}]);


How and why to use the first solution and the second solution?

Sources:

Tanguy Krotoff

unread,
Jan 13, 2013, 12:02:57 PM1/13/13
to ang...@googlegroups.com
btw element.bind('$destroy'...) is being called before scope.$on('$destroy'...)

Tanguy Krotoff

unread,
Jan 26, 2013, 6:16:26 PM1/26/13
to ang...@googlegroups.com
Answering to myself.

I prefer to use element.on('$destroy') as my directives don't always rely on a scope.

Example for Bootstrap datepicker:

      element.on('$destroy', function() {
        element.datepicker('remove');
      });

In order to check if an element still has some jQuery events attached to it:
$._data($(elem).get(0), 'events')
There is also a FireBug extension for that: http://getfirebug.com/wiki/index.php/Firebug_Extensions#Eventbug

Peter Bacon Darwin

unread,
Jan 27, 2013, 10:53:47 AM1/27/13
to ang...@googlegroups.com
If the element is being destroyed and a jQuery plugin is not catching that itself and removing any bindings that it added, then I would call that a bug in the plugin.
AngularJS is very good about destroying a scope that is attached to an element that is being destroyed, including any watches that were set up.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Page Brooks

unread,
Feb 7, 2013, 3:13:06 PM2/7/13
to ang...@googlegroups.com
I had a similar problem where if I used the scope destroy the jQuery.data() objects were destroyed before the plugin had a chance to be destroyed. This caused problems for the plugin. Switching to element destroy fixed the problem.

Sebastien Chartier

unread,
May 7, 2014, 7:15:30 PM5/7/14
to ang...@googlegroups.com
From the Angular.js documentation:

"Note that, in AngularJS, there is also a $destroy jQuery event, which can be used to clean up DOM bindings before an element is removed from the DOM."

Dinh Lam Viet

unread,
May 28, 2014, 5:16:13 AM5/28/14
to ang...@googlegroups.com
.directive('myDirectiveLeak', function ($compile) {
    return {
      replace: 'AE',
      templateUrl: '<div><input type="text"></div>',
      link: function (scope, element) {
          element.datetimepicker();  //// add a Jquery plugin
      }
    };
does my directive cause a memory leak? if have how do I destroy this event? Thank you
Reply all
Reply to author
Forward
0 new messages