Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy scope in $destroy event

Found this code one of the directives in a project I'm working on:

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

Is this code needed? Or can I remove it? Isn't scope destroyed when directive is destroyed?

like image 428
jcubic Avatar asked Mar 20 '26 20:03

jcubic


1 Answers

You don't have to write this code.

As per the docs, the $destroy event is triggered in two cases.

  • Just before a scope is destroyed
  • Just before an element is removed from the DOM

As a side note, you can still use the scope of the element in the element.on('$destroy') event before the scope will be destroyed:

element.on('$destroy', function() {
    console.log('RIP', scope);
});
like image 140
Mistalis Avatar answered Mar 23 '26 12:03

Mistalis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!