使用json响应生成动态列表

时间:2016-10-29 15:06:11

标签: angular typescript ionic-framework ionic2

我有一个像以下一样的json数组;

"collectings": [
    {
      "hint": "OPEN",
      "amount": 24
    },
    {
      "hint": "CREDIT CARD",
      "amount": 347
    },
    {
      "hint": "CASH",
      "amount": 256.5
    }
  ]

现在,我想动态地在列表中显示这些数据,我试着像下面这样做;

<ion-content>
<div class="row" *ngIf="collectings && collectings.length > 0">
  <ion-list>
    <ion-list-header>Comedy</ion-list-header>
    <ion-item *ngFor="let collecting of collectings">{{collectings}}</ion-item>
  </ion-list>
</div>
</ion-content>

但是,它显示在页面上,如下图所示; enter image description here 如何正确显示?

1 个答案:

答案 0 :(得分:3)

而不是{{ collectings }}(这是数组)

<ion-item *ngFor="let collecting of collectings">{{collectings}}</ion-item>

你应该打印collecting属性(没有结尾s)并使用hintamount子属性,如下所示:

 <ion-item *ngFor="let collecting of collectings">
   {{collecting.hint}} - {{ collecting.amount }}
 </ion-item>