diff --git a/components/code-editor/code-editor.service.ts b/components/code-editor/code-editor.service.ts index d9acce5aa9c..40d549a4246 100644 --- a/components/code-editor/code-editor.service.ts +++ b/components/code-editor/code-editor.service.ts @@ -4,8 +4,8 @@ */ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable } from '@angular/core'; -import { BehaviorSubject, Observable, of as observableOf, Subject } from 'rxjs'; +import { Inject, Injectable, OnDestroy } from '@angular/core'; +import { BehaviorSubject, Observable, of as observableOf, Subject, Subscription } from 'rxjs'; import { map, tap } from 'rxjs/operators'; import { CodeEditorConfig, NzConfigService } from 'ng-zorro-antd/core/config'; @@ -29,13 +29,14 @@ function tryTriggerFunc(fn?: (...args: NzSafeAny[]) => NzSafeAny): (...args: NzS @Injectable({ providedIn: 'root' }) -export class NzCodeEditorService { +export class NzCodeEditorService implements OnDestroy { private document: Document; private firstEditorInitialized = false; private loaded$ = new Subject(); private loadingStatus = NzCodeEditorLoadingStatus.UNLOAD; private option: JoinedEditorOptions = {}; private config: CodeEditorConfig; + private subscription: Subscription | null; option$ = new BehaviorSubject(this.option); @@ -46,7 +47,7 @@ export class NzCodeEditorService { this.config = { ...globalConfig }; this.option = this.config.defaultEditorOption || {}; - this.nzConfigService.getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME).subscribe(() => { + this.subscription = this.nzConfigService.getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME).subscribe(() => { const newGlobalConfig: NzSafeAny = this.nzConfigService.getConfigForComponent(NZ_CONFIG_MODULE_NAME); if (newGlobalConfig) { this._updateDefaultOption(newGlobalConfig.defaultEditorOption); @@ -54,6 +55,11 @@ export class NzCodeEditorService { }); } + ngOnDestroy(): void { + this.subscription!.unsubscribe(); + this.subscription = null; + } + private _updateDefaultOption(option: JoinedEditorOptions): void { this.option = { ...this.option, ...option }; this.option$.next(this.option);