Skip to content

Instantly share code, notes, and snippets.

@munkacsitomi
Created March 11, 2019 11:59
Show Gist options
  • Save munkacsitomi/9d81aee0556401f22de4f07fa730b40b to your computer and use it in GitHub Desktop.
Save munkacsitomi/9d81aee0556401f22de4f07fa730b40b to your computer and use it in GitHub Desktop.
Angular event and listener examples in a modal component
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