Skip to content

Instantly share code, notes, and snippets.

@kerimovscreations
Last active August 21, 2019 12:20
Show Gist options
  • Save kerimovscreations/a43326171097e608b0ffdec796f60cce to your computer and use it in GitHub Desktop.
Save kerimovscreations/a43326171097e608b0ffdec796f60cce to your computer and use it in GitHub Desktop.
Click outside directive testing example
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