使用ngFor迭代Json对象

问题描述 投票:3回答:1

我从网络服务器获得以下JSON字符串。

[
{"name":"Joe Lincoln","age":"42","grade":"9","active":"1"},
{"name":"Jack Smith","age":"38","grade":"8","active":"1"},
{"name":"Peter Smith","age":"42","grade":"9","active":"0"},
{"name":"Eva Lorens","age":"42","grade":"8","active":"1"},
]

我希望公开HTML中的JSON,我尝试了以下但是字段没有被填充,我也得到No Exception,所以我很难找出它为什么不起作用。

TS

  this.servletService.webserviceCall('MemberUnit', 'getMembers',  params).then((obs)=>{
      obs.subscribe(
          (data) => {
            this.members =  JSON.parse(data);
          }); 
    }) 

HTML

 <ion-item *ngFor="let member of members">
 <ion-avatar item-left>
       <svg width="75px" height="75px" >
   <text x="50%" y="50%" text-anchor="middle" stroke="#2980b9" stroke-width="2px" dy=".1em">{{member.grade }}</text>
      </svg>
      </ion-avatar>
       <h2>{{member.name}}</h2>
      <h3>{{member.age}}</h3>
    </ion-item>
json angular ionic2
1个回答
3
投票

您可以使用Object.keys从对象获取密钥(在IE AFAIK中需要polyfill)

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'keys' })
export class KeysPipe implements PipeTransform {
  transform(value): any {
    if(!value) return null;
    return Object.keys(value);
  }
}
<div *ngFor="let key of member | keys">{{member[key]}}</div>

对于Angular 6,请参阅How to iterate object keys using *ngFor

© www.soinside.com 2019 - 2024. All rights reserved.