This problem has been driving me nuts. I have two components which both are toggleable dropdowns on the same page. I have something like this for both components:
@HostListener('click', ['$event'])
clickInside() {
event.stopPropagation();
this.showMenu = true;
}
@HostListener('document:click')
clickOutside() {
if (this.showMenu) {
this.showMenu = false;
}
}
It works just fine for one component but when I have two components on the same page then clicking the other component will leave the first component open and the result is that they are both open at the same time which I do not want. The reason is of course the event.stopPropagation() but without this, a click inside the component will also be a click inside the document.