E
E
Egor2014-09-28 14:41:45
Angular
Egor, 2014-09-28 14:41:45

Angular: Will a directive be garbage collected after the object the directive is bound to disappears?

The directive is bound to the element.

app.directive('myDirective',function(){
return{
   restrict:'ACE',
   link: function(scope, element){
     element.bind('click',function(){
      doSomething();
     });
   } 
}
});

<input type="text" my-directive value="">
Then there is a transition to a new page, and the element to which the directive was attached is no longer in the DOM.
Question: do I need to do unbind in the directive or will the garbage collector deal with it after the disappearance of the input from the DOM?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-09-28
@ByKraB

The browser (webview) will take care of this, because if there is no directive, then there is no element to which it is attached. Therefore, the browser itself will remove all event handlers along with the element being removed.
If the directive hangs handlers on something higher ($window, $document for example) then yes, then you need to do unbind on the $destroy event of $scope.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question