Last active
August 21, 2019 12:20
-
-
Save kerimovscreations/a43326171097e608b0ffdec796f60cce to your computer and use it in GitHub Desktop.
Click outside directive testing example
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
import { Directive, ElementRef, Output, HostListener, EventEmitter } from '@angular/core'; | |
@Directive({ | |
selector: '[appClickOutside]' | |
}) | |
export class ClickOutsideDirective { | |
constructor(private elementRef: ElementRef) { } | |
@Output() | |
public appClickOutside = new EventEmitter(); | |
@HostListener('document:click', ['$event.target']) | |
public onClick(targetElement) { | |
const clickedInside = this.elementRef.nativeElement.contains(targetElement); | |
if (!clickedInside) { | |
this.appClickOutside.emit(null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment