I would like to know whenever click is triggered inside parent div #menu, irrelavant of html tags exists inside it.
nativeElement.parent didn't worked for me.
HTML code:
<button #toggleButton (click)="toggleMenu()"> Toggle Menu</button>
<div class="menu" *ngIf="isMenuOpen" #menu>
<div>
I'm the menu. Click outside to close me
</div>
</div>
Angular script:
this.renderer.listen('window', 'click',(e:Event)=>{
if(e.target !== this.toggleButton.nativeElement && e.target!==this.menu.nativeElement){
this.isMenuOpen=false;
}
});
This is not working.
Add Host to component meta tag.
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
host: {
"(window:click)": "onClick()"
}
})
stopPropagation()
The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases.
toggleMenu($event) {
$event.stopPropagation();
this.isMenuOpen = !this.isMenuOpen;
}
onClick() {
this.isMenuOpen = false;
}
Example:https://stackblitz.com/edit/angular-oenkbw
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