【问题标题】:Angular4: Concatenate string to form dynamic key in templateAngular4:连接字符串以在模板中形成动态键
【发布时间】:2017-06-30 15:09:32
【问题描述】:

我正在尝试根据 *ngFor 索引在模板内动态创建一个键。

<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" }]
   }
]

【问题讨论】:

  • 有什么问题?
  • 我希望每个循环中的键是索引的串联。

标签: angular templates variables key concatenation


【解决方案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>

【讨论】:

  • 这很有趣,但不幸的是出错了。 HighlightTableComponent.html:6 错误类型错误:无法读取未定义的属性“0”
  • 你是个天才,这行得通。 {{ 行['r' + j + 'c' + i] }}
猜你喜欢
  • 2015-06-06
  • 2014-09-05
  • 1970-01-01
  • 2011-06-09
  • 2020-06-03
  • 2011-06-18
  • 1970-01-01
  • 2011-05-22
相关资源
最近更新 更多