Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate a Json Object with ngFor

I get from a webserver the following JSON String.

[
{"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"},
]

I want public the JSON in HTML, I tried the following but the fields get not be filled, I also get No Exception, so its very difficult for me to find out why its not working.

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>
like image 582
Sandruna Avatar asked Feb 16 '17 15:02

Sandruna


1 Answers

You can get the keys from an object using Object.keys (requires polyfill in IE AFAIK)

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>

For Angular 6 see How to iterate object keys using *ngFor

like image 154
Günter Zöchbauer Avatar answered Oct 06 '22 01:10

Günter Zöchbauer