Created
February 21, 2022 09:00
-
-
Save Mustafa-Omran/c3bf218ade5c175ec37fb8d913279255 to your computer and use it in GitHub Desktop.
Angular - Custom directive when clicking outside specific element
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
@Directive({ | |
selector: '[clickOutsideElement]' | |
}) | |
export class ClickOutsideDirective { | |
@Output() clickOutside = new EventEmitter<void>(); | |
constructor(private elementRef: ElementRef) { } | |
@HostListener('document:click', ['$event.target']) | |
onClickOutsideElement(target) { | |
const clickedInside = this.elementRef.nativeElement.contains(target); | |
if (!clickedInside) { | |
this.clickOutside.emit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment