diff --git a/components/modal/modal-ref.ts b/components/modal/modal-ref.ts index e81f3135f1..b8bbc9391a 100644 --- a/components/modal/modal-ref.ts +++ b/components/modal/modal-ref.ts @@ -7,7 +7,7 @@ import { ESCAPE, hasModifierKey } from '@angular/cdk/keycodes'; import { OverlayRef } from '@angular/cdk/overlay'; import { EventEmitter } from '@angular/core'; import { Subject } from 'rxjs'; -import { filter, take } from 'rxjs/operators'; +import { filter, take, takeUntil } from 'rxjs/operators'; import { NzSafeAny } from 'ng-zorro-antd/core/types'; import { isPromise } from 'ng-zorro-antd/core/util'; @@ -36,6 +36,8 @@ export class NzModalRef implements NzModalLegacyAP private closeTimeout?: number; + private destroy$ = new Subject(); + constructor( private overlayRef: OverlayRef, private config: ModalOptions, @@ -64,7 +66,7 @@ export class NzModalRef implements NzModalLegacyAP this._finishDialogClose(); }); - containerInstance.containerClick.pipe(take(1)).subscribe(() => { + containerInstance.containerClick.pipe(take(1), takeUntil(this.destroy$)).subscribe(() => { const cancelable = !this.config.nzCancelLoading && !this.config.nzOkLoading; if (cancelable) { this.trigger(NzTriggerAction.CANCEL); @@ -88,9 +90,11 @@ export class NzModalRef implements NzModalLegacyAP this.trigger(NzTriggerAction.CANCEL); }); - containerInstance.cancelTriggered.subscribe(() => this.trigger(NzTriggerAction.CANCEL)); + containerInstance.cancelTriggered + .pipe(takeUntil(this.destroy$)) + .subscribe(() => this.trigger(NzTriggerAction.CANCEL)); - containerInstance.okTriggered.subscribe(() => this.trigger(NzTriggerAction.OK)); + containerInstance.okTriggered.pipe(takeUntil(this.destroy$)).subscribe(() => this.trigger(NzTriggerAction.OK)); overlayRef.detachments().subscribe(() => { this.afterClose.next(this.result); @@ -197,5 +201,6 @@ export class NzModalRef implements NzModalLegacyAP _finishDialogClose(): void { this.state = NzModalState.CLOSED; this.overlayRef.dispose(); + this.destroy$.next(); } }