2017-06-30 6 views
0

* ngForインデックスからテンプレート内にキーを動的に作成しようとしています。Angular4:文字列を連結してテンプレート内に動的キーを形成する

<div *ngFor="let col of table; let i = index"> 
    <span *ngFor="let row of col; let j = index"> 
    {{ row.ricj }} 
    </span> 
</div> 

"ricj"の "i"と "j"は実際にはインデックス "i"と "j"です。これは可能ですか?

makeTable(){ 
    for(this.c = 0; this.c < this.columnslength; this.c++){ 
     for(this.r = 0; this.r < this.rowslength; this.r++){ 
     this.key = "r" + this.r + "c" + this.c; 
     this.cell = { [this.key]: this.key }; 
     this.cols.push(this.cell); 
     } 
     this.table.push(this.cols); 
     this.cols = []; 
    } 
} 

テーブルオブジェクトは、次のようになります

元のテーブルオブジェクトがこのように構築され、

[ 
    { 
     [{ "r0c0": "r0c0" }, { "r1c0": "r1c0" }] 
    }, 
    { 
     [{ "r0c1": "r0c1" }, { "r1c1": "r1c1" }] 
    } 
] 
+0

、これを試してみてください? – alehn96

+0

各ループのキーをインデックスの連結にします。 –

答えて

1

は問題が何であるかを

<div *ngFor="let col of table; let i = index"> 
    <span *ngFor="let row of col; let j = index"> 
     {{ row['r' + j + 'c' + i] }} 
    </span> 
    </div> 
+0

これは非常に興味深いですが、残念なことにエラーが発生します。 HighlightTableComponent.html:6エラーTypeError:未定義のプロパティ '0'を読み取ることができません –

+0

あなたは天才です、これは動作します。 {{行['r' + j + 'c' + i]}} –

+0

回答をお寄せください –

関連する問題