I want to attach an event to the document. when document is clicked I want to remove my directive element. but it is firing for all past instances also.
Below is the code:
link: function(scope, iElement, iAttrs) {
$document.on('click',function(){
iElement.slideUp('fast',function(){
$(this).remove();
});
});
}
Try to remove the handler in the destroy handelr
link: function (scope, iElement, iAttrs) {
function handler() {
iElement.slideUp('fast', function () {
$(this).remove();
});
}
$document.on('click', handler);
scope.$on('$destroy', function () {
$document.off('click', handler);
})
}
When you add jQuery events like this, the event will stay alive even after the directive is destroyed. You have to specifically turn the event off like this:
link: function(scope, iElement, iAttrs) {
$document.on('click',function(){
iElement.slideUp('fast',function(){
$(this).remove();
$document.off('click');
});
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With