【问题标题】:How to get the length of the json object in Angular 2 template如何在Angular 2模板中获取json对象的长度
【发布时间】:2018-07-19 20:31:42
【问题描述】:

我有一个这样的 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>

在上面的代码中,我如何获得lengthvariation 数据。还需要将所有新字段和旧字段值渲染并显示到视图中。 谁能帮帮我

【问题讨论】:

  • 使用data['variations'].length
  • 它显示如下 [object Object]

标签: html json angular angular2-template


【解决方案1】:

组件端:

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

【讨论】:

  • ERROR TypeError: _co.objectKeys is not a function
  • @Ajmalsha,请查看工作演示;和objectKeys = Object.keys; 把它放在组件中
  • 是角2还是角4?
  • Angular 5 ,它可以与任何 angular2/+ 版本一起使用,因为它的非 Angular 特性是纯 JavaScript。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 2019-09-22
  • 2017-07-29
相关资源
最近更新 更多