Setsuna
Setsuna

Reputation: 2141

What is the proper way to clean up/destroy directives?

Assuming I have multiple directives consisting of something like:

<mydirective id="1">...template content...</mydirective>
<mydirective id="2">...template content...</mydirective>

How do you go about destroying the directive? Usually I would do something in jquery where I $('#2').remove();

Aside from removing the specific directive from the dom, is there something else that has to be done, or some other way to destroy the dom element so you do not leave anything other "garbage" lingering? (i.e. orphan controllers, etc.)

Upvotes: 3

Views: 4608

Answers (1)

Maurice
Maurice

Reputation: 27632

If you need to do any cleanup work in a directive you can subscribe to the $destroy event.

In your link function:

element.on('$destroy', function() {
    // Do cleanup work
});

Upvotes: 11

Related Questions