如何在Angular 2模板

时间:2018-02-09 05:56:54

标签: html json angular angular2-template

我有一个像这样的json数组。

[
  {
    "variations": {
      "title": {
        "newValue": "test new value1",
        "oldValue": "test old value1"
      }
    }
  },
  {
    "variations": {
      "data": {
        "newValue": "new data",
        "oldValue": "httpold data"
      },
      "length": {
        "newValue": "48",
        "oldValue": "9.00"
      }
    }
  }
]

从这里我想得到Html变化的长度,以及渲染和值来查看。

<table class="table table-bordered table-hover">
                        <thead class="table-title">
                            <tr>
                                <th class="text-center">Field count</th>
                                <th class="text-center">Field Name</th>
                                <th class="text-center">Old Value</th>
                                <th class="text-center">New Value</th>
                            </tr>
                        </thead>
                        <tbody *ngFor="let data of datas">
                            <tr>
                                <td class="text-left" rowspan="2">{{data.variations.length}}</td>
                                <td class="text-left" rowspan="2"></td>
                                <td class="text-left" rowspan="2"></td>
                            </tr>
                            <tr>
                            </tr>
                        </tbody>
                    </table>

在上面的代码中,我如何获得length variation个数据。还需要渲染并向视图显示所有新字段和旧字段值。 任何人都可以帮助我

1 个答案:

答案 0 :(得分:4)

组件方:

objectKeys = Object.keys;

模板面:

<tbody *ngFor="let data of datas">
    <tr>
        <td class="text-left" rowspan="2">{{objectKeys(data.variations).length}}</td>
        ...
    </tr>
    ...
</tbody>

<强> WORKING DEMO