Created
March 11, 2019 11:59
-
-
Save munkacsitomi/9d81aee0556401f22de4f07fa730b40b to your computer and use it in GitHub Desktop.
Angular event and listener examples in a modal 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
export class ModalComponent { | |
readonly escapeKeyCode = 27; | |
@Output() modalClosed = new EventEmitter(); | |
@HostListener('document:click', ['$event']) onClickHandler(event) { | |
if (this.checkBackgroundClass(event.target, 'transparent-background')) { | |
this.close(); | |
} | |
} | |
@HostListener('document:keydown', ['$event']) onKeydownHandler(event: KeyboardEvent) { | |
if (event.keyCode === this.escapeKeyCode) { | |
this.close(); | |
} | |
} | |
close = () => this.modalClosed.emit(true); | |
checkBackgroundClass = (element, className): boolean => element.classList && element.classList.contains(className); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment