* ngFor不使用动态值

时间:2016-03-07 15:14:22

标签: json angular ngfor

处理一些数组项目,但坚持* 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(这是该类别中的第一个索引)。我怎么能做到这一点。

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

  2. 有时* ngFor和* ngIf在嵌入同一个元素时工作不正常?

  3. 这是我的workground demo

1 个答案:

答案 0 :(得分: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