Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk/collections): recycle repeater strategy retaining references to destroyed views #21744

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/cdk/collections/recycle-view-repeater-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte
for (const view of this._viewCache) {
view.destroy();
}
this._viewCache = [];
}

/**
Expand All @@ -101,7 +102,7 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte
private _insertView(viewArgsFactory: () => _ViewRepeaterItemInsertArgs<C>, currentIndex: number,
viewContainerRef: ViewContainerRef,
value: T): EmbeddedViewRef<C> | undefined {
let cachedView = this._insertViewFromCache(currentIndex!, viewContainerRef);
const cachedView = this._insertViewFromCache(currentIndex!, viewContainerRef);
if (cachedView) {
cachedView.context.$implicit = value;
return undefined;
Expand All @@ -114,15 +115,14 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte

/** Detaches the view at the given index and inserts into the view cache. */
private _detachAndCacheView(index: number, viewContainerRef: ViewContainerRef) {
const detachedView = this._detachView(index, viewContainerRef);
const detachedView = viewContainerRef.detach(index) as EmbeddedViewRef<C>;
this._maybeCacheView(detachedView, viewContainerRef);
}

/** Moves view at the previous index to the current index. */
private _moveView(adjustedPreviousIndex: number, currentIndex: number,
viewContainerRef: ViewContainerRef, value: T): EmbeddedViewRef<C> {
const view = viewContainerRef.get(adjustedPreviousIndex!) as
EmbeddedViewRef<C>;
const view = viewContainerRef.get(adjustedPreviousIndex!) as EmbeddedViewRef<C>;
viewContainerRef.move(view, currentIndex);
view.context.$implicit = value;
return view;
Expand Down Expand Up @@ -159,9 +159,4 @@ export class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemConte
}
return cachedView || null;
}

/** Detaches the embedded view at the given index. */
private _detachView(index: number, viewContainerRef: ViewContainerRef): EmbeddedViewRef<C> {
return viewContainerRef.detach(index) as EmbeddedViewRef<C>;
}
}