Last active
August 19, 2018 17:25
-
-
Save hiepxanh/c7a174e89d06fcdb11bef2c5539f530e to your computer and use it in GitHub Desktop.
check-element inside or not with angular https://stackoverflow.com/questions/40107008/detect-click-outside-angular-component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from here: | |
// @HostListener('document:click', ['$event']) | |
// clickout(event) { | |
// console.log("target?",event.target); | |
// if(this.eRef.nativeElement.contains(event.target)) { | |
// console.log("clicked inside",event); | |
// } else { | |
// console.log("clicked outside",event); | |
// } | |
// } | |
// upgrade 1: | |
// @ViewChild('confirm') confirm: ElementRef; | |
// @ViewChild('pending') pending: ElementRef; | |
// @ViewChild('statistic') statistic: ElementRef; | |
// @ViewChild('notification') notification: ElementRef; | |
// @HostListener('document:click', ['$event']) | |
// clickout(event) { | |
// let result = | |
// event.path.includes(this.confirm.nativeElement) || | |
// event.path.includes(this.pending.nativeElement) || | |
// event.path.includes(this.statistic.nativeElement) || | |
// event.path.includes(this.notification.nativeElement) | |
// console.log('includes 1 in 4 buttons?',result) | |
// if(result) { | |
// console.log("clicked inside",event); | |
// } else { | |
// console.log("clicked outside",event); | |
// } | |
// } | |
// upgrarde 2: | |
@ViewChild('confirm') confirm: ElementRef; | |
@ViewChild('pending') pending: ElementRef; | |
@ViewChild('statistic') statistic: ElementRef; | |
@ViewChild('notification') notification: ElementRef; | |
@HostListener('document:click', ['$event']) | |
clickout(event) { | |
if (event.path.includes(this.confirm.nativeElement)) this.toogleItem(this.status.confirm) | |
else if (event.path.includes(this.pending.nativeElement)) this.toogleItem(this.status.pending) | |
else if (event.path.includes(this.statistic.nativeElement)) this.toogleItem(this.status.statistic) | |
else if (event.path.includes(this.notification.nativeElement)) this.toogleItem(this.status.notification) | |
else this.toogleItem({name:''}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment