Skip to content

Commit 1017897

Browse files
committed
fix(cdk/scrolling): avoid injector errors
Fixes some "Injector is already destroyed" errors.
1 parent 996d44d commit 1017897

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/cdk/scrolling/virtual-scroll-viewport.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ChangeDetectionStrategy,
1515
ChangeDetectorRef,
1616
Component,
17+
DestroyRef,
1718
ElementRef,
1819
inject,
1920
Inject,
@@ -201,6 +202,17 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On
201202
this.elementRef.nativeElement.classList.add('cdk-virtual-scrollable');
202203
this.scrollable = this;
203204
}
205+
206+
this._injector.get(DestroyRef).onDestroy(() => {
207+
this._isDestroyed = true;
208+
this.detach();
209+
this._scrollStrategy.detach();
210+
211+
// Complete all subjects
212+
this._renderedRangeSubject.complete();
213+
this._detachedSubject.complete();
214+
this._viewportChanges.unsubscribe();
215+
});
204216
}
205217

206218
override ngOnInit() {
@@ -242,20 +254,6 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On
242254
);
243255
}
244256

245-
override ngOnDestroy() {
246-
this.detach();
247-
this._scrollStrategy.detach();
248-
249-
// Complete all subjects
250-
this._renderedRangeSubject.complete();
251-
this._detachedSubject.complete();
252-
this._viewportChanges.unsubscribe();
253-
254-
this._isDestroyed = true;
255-
256-
super.ngOnDestroy();
257-
}
258-
259257
/** Attaches a `CdkVirtualScrollRepeater` to this viewport. */
260258
attach(forOf: CdkVirtualScrollRepeater<any>) {
261259
if (this._forOf && (typeof ngDevMode === 'undefined' || ngDevMode)) {

tools/public_api_guard/cdk/scrolling.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On
197197
// (undocumented)
198198
static ngAcceptInputType_appendOnly: unknown;
199199
// (undocumented)
200-
ngOnDestroy(): void;
201-
// (undocumented)
202200
ngOnInit(): void;
203201
get orientation(): "horizontal" | "vertical";
204202
set orientation(orientation: 'horizontal' | 'vertical');

0 commit comments

Comments
 (0)