2017-11-15 20 views
0

これは角度の質問が多いかもしれませんが、データを読み込んだ後で明示的なデータグリッドのサイズを変更しようとしていますが(列を動的に表示/隠しています)、エラーは継続します明瞭度のデータグリッドへの参照をtypescriptから取得する方法

Uncaught TypeError: Cannot read property 'resize' of undefined

component.html

<clr-datagrid #contactsGrid> 
[...] 
</clr-datagrid> 

component.ts

import { Component, OnInit, ViewChild } from '@angular/core'; 
import { Datagrid } from "clarity-angular"; 

export class GridComponent implements OnInit { 
    @ViewChild('contactsGrid') datagrid: Datagrid; 
    //@ViewChild('Datagrid') datagrid: Datagrid; //This doesn't work either 

    constructor() { } 

    ngOnInit() { 
     [...] 
     this.datagrid.resize(); //this.datagrid is undefined 
    } 
} 

答えて

2

それが表示されます。上記のサンプルコードは正しいですが、おそらくもっと進んでいますか?実際の問題を実演していることを示す完全な例を共有していますか?

ここでは、データグリッドの参照を取得するデモを見ることができます。 https://stackblitz.com/edit/clarity-datagrid-brfwx9?file=app/launches/launches.component.ts

テンプレートがあります。

<clr-datagrid [(clrDgSelected)]="selected" #datagridRef> 

その後コントローラがあります。

export class LaunchesComponent { 
    @ViewChild('datagridRef') datagrid: Datagrid; 

    ngOnInit() { 
    console.log(this.datagrid); // Defined 
    } 

あなたが唯一のデータグリッドを持っている場合は、HTML

にフックを追加する必要はありません
export class LaunchesComponent { 
    @ViewChild(Datagrid) datagrid: Datagrid; 

    ngOnInit() { 
    console.log(this.datagrid); // Defined 
    } 

あなたのコードが'Datagrid'(文字列)のインスタンスを渡していたようですDatagridの広告(コンストラクタへの参照)

+0

データグリッドに* ngIfがありましたが、これは問題のようです。ありがとう。 – Jake

関連する問題