From e774692ba060cfa1334a3599a410f2a59eb24d95 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 14 May 2019 15:40:45 +0200 Subject: [PATCH] fix(table): text-column throws if name input is set eagerly with ivy Currently when someone uses the `CdkTextColumn` directive and sets the `name` input binding to a static value, Ivy will set the input value eagerly in the creation phase where the static query results are not available yet. This causes a runtime exception since the `this.columnDef` variable is still set to undefined when the `name` input setter executes. --- src/cdk/table/text-column.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/cdk/table/text-column.ts b/src/cdk/table/text-column.ts index a65abffe799..aa37a38997c 100644 --- a/src/cdk/table/text-column.ts +++ b/src/cdk/table/text-column.ts @@ -78,7 +78,10 @@ export class CdkTextColumn implements OnDestroy, OnInit { } set name(name: string) { this._name = name; - this.columnDef.name = name; + + // With Ivy, inputs can be initialized before static query results are + // available. In that case, we defer the synchronization until "ngOnInit" fires. + this._syncColumnDefName(); } _name: string; @@ -127,6 +130,8 @@ export class CdkTextColumn implements OnDestroy, OnInit { } ngOnInit() { + this._syncColumnDefName(); + if (this.headerText === undefined) { this.headerText = this._createDefaultHeaderText(); } @@ -165,4 +170,11 @@ export class CdkTextColumn implements OnDestroy, OnInit { return this.name[0].toUpperCase() + this.name.slice(1); } + + /** Synchronizes the column definition name with the text column name. */ + private _syncColumnDefName() { + if (this.columnDef) { + this.columnDef.name = this.name; + } + } }