Created
April 30, 2020 19:44
-
-
Save jagomf/cb174e287bc1e8d8a7efbf8532ff176e to your computer and use it in GitHub Desktop.
Detect click outside an Angular component
This file contains hidden or 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
@Component({ ... }) | |
export class Mva10SelectFieldComponent { | |
constructor( private elementRef: ElementRef ) { } | |
onClickOut(targetElement?: HTMLElement): void { | |
if (!targetElement) { return; } | |
const clickedInside = this.elementRef.nativeElement.contains(targetElement); | |
if (!clickedInside) { | |
// Clicked outside component! | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment