-
-
Save CobyBoy/d3b530ccd9abc7580befbfce98f477ce to your computer and use it in GitHub Desktop.
angular 4 directive that emit an event when click event happen outside of target
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
import {Directive, ElementRef, Output, EventEmitter, HostListener} from '@angular/core'; | |
@Directive({ | |
selector: '[appClickOutside]' | |
}) | |
export class ClickOutsideDirective { | |
constructor(private _elementRef: ElementRef) { | |
} | |
@Output() | |
public clickOutside = new EventEmitter(); | |
@HostListener('document:click' , ['$event.target']) onClick( target) { | |
const clickedInside = this._elementRef.nativeElement.contains(target); | |
if (!clickedInside) { | |
this.clickOutside.emit(null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment