Skip to content

Commit df1ddee

Browse files
crisbetokara
authored andcommitted
perf(table): cell references not being cleaned up on destroy (#5809)
1 parent 0adb2ab commit df1ddee

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/cdk/table/table.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ describe('CdkTable', () => {
155155
expect(changedRows[2].getAttribute('initialIndex')).toBe(null);
156156
});
157157

158+
it('should clear the row view containers on destroy', () => {
159+
const rowPlaceholder = fixture.componentInstance.table._rowPlaceholder.viewContainer;
160+
const headerPlaceholder = fixture.componentInstance.table._headerRowPlaceholder.viewContainer;
161+
162+
spyOn(rowPlaceholder, 'clear').and.callThrough();
163+
spyOn(headerPlaceholder, 'clear').and.callThrough();
164+
165+
fixture.destroy();
166+
167+
expect(rowPlaceholder.clear).toHaveBeenCalled();
168+
expect(headerPlaceholder.clear).toHaveBeenCalled();
169+
});
170+
158171
describe('with trackBy', () => {
159172

160173
let trackByComponent: TrackByCdkTableApp;

src/cdk/table/table.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
TrackByFunction,
2828
ViewChild,
2929
ViewContainerRef,
30-
ViewEncapsulation
30+
ViewEncapsulation,
3131
} from '@angular/core';
3232
import {CollectionViewer, DataSource} from './data-source';
3333
import {CdkCellOutlet, CdkCellOutletRowContext, CdkHeaderRowDef, CdkRowDef} from './row';
@@ -175,8 +175,11 @@ export class CdkTable<T> implements CollectionViewer {
175175
}
176176

177177
ngOnDestroy() {
178+
this._rowPlaceholder.viewContainer.clear();
179+
this._headerRowPlaceholder.viewContainer.clear();
178180
this._onDestroy.next();
179181
this._onDestroy.complete();
182+
180183
if (this.dataSource) {
181184
this.dataSource.disconnect(this);
182185
}
@@ -343,7 +346,7 @@ export class CdkTable<T> implements CollectionViewer {
343346
viewRef.context.first = index === 0;
344347
viewRef.context.last = index === count - 1;
345348
viewRef.context.even = index % 2 === 0;
346-
viewRef.context.odd = index % 2 !== 0;
349+
viewRef.context.odd = !viewRef.context.even;
347350
}
348351
}
349352

0 commit comments

Comments
 (0)