Let say I want to alert user every time he clicks on <a>
tag.
document.querySelector('body').addEventListener('click', function (event : any) {
if (event.target.tagName === 'A') {
event.preventDefault();
alert('Congratulations! A tag clicked!!!');
}
});
If I put this binding in constructor
(on in ngOnInit()
) I will have new binding (and one more alert) every time i go to this component (router link).
What is the way to bind only one time, on the first init?
In the meantime I use removeEventListener()
in ngOnDestroy()
. It seems to be ugly. Is there anything better?