From 6b4eec1d8473f448912dd062781d59a3a7d03387 Mon Sep 17 00:00:00 2001 From: wenqi73 <1264578441@qq.com> Date: Wed, 28 Nov 2018 01:55:56 +0800 Subject: [PATCH] fix: move listener on document --- components/modal/nz-modal.component.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/components/modal/nz-modal.component.ts b/components/modal/nz-modal.component.ts index 1dc74c1ea5e..b790b2f1525 100644 --- a/components/modal/nz-modal.component.ts +++ b/components/modal/nz-modal.component.ts @@ -24,7 +24,7 @@ import { ViewContainerRef } from '@angular/core'; -import { Observable, Subject } from 'rxjs'; +import { fromEvent, Observable, Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { NzMeasureScrollbarService } from '../core/services/nz-measure-scrollbar.service'; @@ -111,12 +111,6 @@ export class NzModalComponent extends NzModalRef impleme @ViewChild('bodyContainer', { read: ViewContainerRef }) bodyContainer: ViewContainerRef; @Input() @InputBoolean() nzKeyboard: boolean = true; - @HostListener('keydown', [ '$event' ]) - public onKeyDown(event: KeyboardEvent): void { - if (event.keyCode === ESCAPE && this.nzKeyboard) { - this.onClickOkCancel('cancel'); - } - } get hidden(): boolean { return !this.nzVisible && !this.animationState; @@ -149,6 +143,7 @@ export class NzModalComponent extends NzModalRef impleme ngOnInit(): void { this.i18n.localeChange.pipe(takeUntil(this.unsubscribe$)).subscribe(() => this.locale = this.i18n.getLocaleData('Modal')); + fromEvent(this.document.body, 'keydown').pipe(takeUntil(this.unsubscribe$)).subscribe((e: KeyboardEvent) => this.keydownListener(e)); if (this.isComponent(this.nzContent)) { this.createDynamicComponent(this.nzContent as Type); // Create component along without View @@ -205,6 +200,12 @@ export class NzModalComponent extends NzModalRef impleme }); } + keydownListener(event: KeyboardEvent): void { + if (event.keyCode === ESCAPE && this.nzKeyboard) { + this.onClickOkCancel('cancel'); + } + } + open(): void { this.changeVisibleFromInside(true); }