diff --git a/src/cdk/table/table.spec.ts b/src/cdk/table/table.spec.ts index b69724ec3c48..9850d8cd6f06 100644 --- a/src/cdk/table/table.spec.ts +++ b/src/cdk/table/table.spec.ts @@ -155,6 +155,19 @@ describe('CdkTable', () => { expect(changedRows[2].getAttribute('initialIndex')).toBe(null); }); + it('should clear the row view containers on destroy', () => { + const rowPlaceholder = fixture.componentInstance.table._rowPlaceholder.viewContainer; + const headerPlaceholder = fixture.componentInstance.table._headerRowPlaceholder.viewContainer; + + spyOn(rowPlaceholder, 'clear').and.callThrough(); + spyOn(headerPlaceholder, 'clear').and.callThrough(); + + fixture.destroy(); + + expect(rowPlaceholder.clear).toHaveBeenCalled(); + expect(headerPlaceholder.clear).toHaveBeenCalled(); + }); + describe('with trackBy', () => { let trackByComponent: TrackByCdkTableApp; diff --git a/src/cdk/table/table.ts b/src/cdk/table/table.ts index 592e56d63fa6..89b328857f83 100644 --- a/src/cdk/table/table.ts +++ b/src/cdk/table/table.ts @@ -27,7 +27,7 @@ import { TrackByFunction, ViewChild, ViewContainerRef, - ViewEncapsulation + ViewEncapsulation, } from '@angular/core'; import {CollectionViewer, DataSource} from './data-source'; import {CdkCellOutlet, CdkCellOutletRowContext, CdkHeaderRowDef, CdkRowDef} from './row'; @@ -175,8 +175,11 @@ export class CdkTable implements CollectionViewer { } ngOnDestroy() { + this._rowPlaceholder.viewContainer.clear(); + this._headerRowPlaceholder.viewContainer.clear(); this._onDestroy.next(); this._onDestroy.complete(); + if (this.dataSource) { this.dataSource.disconnect(this); } @@ -343,7 +346,7 @@ export class CdkTable implements CollectionViewer { viewRef.context.first = index === 0; viewRef.context.last = index === count - 1; viewRef.context.even = index % 2 === 0; - viewRef.context.odd = index % 2 !== 0; + viewRef.context.odd = !viewRef.context.even; } }