首页 文章

* ngFor不使用动态值

提问于
浏览
0

处理一些数组项目,但坚持在* ngFor不接受另一个 *ngFor 提供的动态值的地方 .

<table border="2">
  <tr>
    <th rowspan="2">Subject</th>
    <th colspan="4" *ngFor='#category of json.category_name'>{{category}}</th>
    <th colspan="4">Overall</th>
  </tr>
  <tr>
    <div *ngFor="#category of json.category_name">
      <th *ngFor="#fa of json.total_fa.category">{{fa}}</th>  //Not Working
    </div>
  </tr>
</table>

这里在注释行中我想提供 json.total_fa.category ,其中 category 来自另一个 *ngFor ,它在上面声明 . 但是没有得到结果似乎有角度试图找出 json.total_fa.category 本身不存在 .

但我想加载 json.total_fa_term1 (这是该类别的第一个索引) . 我怎么能做到这一点 .

  • 我可以在同一个HTML组件上使用多个* ngFor吗?

  • 有时* ngFor和* ngIf在嵌入相同元素时工作不正常的原因?

这是我的workground demo

1 回答

  • 2

    您需要使用 .total_fa[category] 而不是 .total_fa.category ,否则您将尝试访问名为 category 的属性:

    <div *ngFor="#category of json.category_name">
      <th *ngFor="#fa of json.total_fa[category]">{{fa}}</th>
    </div>
    

    请参阅更新的plunkr:http://plnkr.co/edit/v08AdzNsR4GHMlZWQNsR?p=preview .

相关问题