在ionic中遍历JSON数组以显示所有数据

时间:2019-06-18 10:10:51

标签: json angular ionic-framework

1-JSON数据结果的图像链接

我在数组中有JSON数据,并且可以在HTML代码中显示数组的单个部分。但是,我无法显示诸如在完整数组中显示所有名称的信息(例如,我只能访问array[0]

ts文件

patients: any[] = [];

this.viewService.viewPatient().subscribe(res => {
      this.patients = [res];
      console.log('data');
      console.log(res);
    });

html文件

 // This works
 <ion-item *ngFor="let patient of patients">
       Name: {{patient.patients.data[0].FirstName}}
 </ion-item>

 // Doesnt work
 <ion-item *ngFor="let patient of patients"> (1) 
        Name: {{patient.patients.data.FirstName}}
 </ion-item>

viewpatient()

viewPatient() {
    return this.http.get(apiUrl + 'viewPatient.php');
  }

2 个答案:

答案 0 :(得分:2)

viewPatient() api中将res.patients.data直接分配给this。Patients

this.viewService.viewPatient().subscribe(res => {
      this.patients = res.patients.data;
      console.log('data');
      console.log(res);
    });

然后按照以下说明使用模板

<ion-item *ngFor="let patient of patients">
       Name: {{patient.FirstName}}
 </ion-item>

答案 1 :(得分:1)

尝试这样:

<ion-item *ngFor="let patient of patients?.patients?.data">
       Name: {{patient.FirstName}}
</ion-item>

或者您可以直接在data中分配patients数组

TS:

patients: any[] = [];

this.viewService.viewPatient().subscribe(res => {
     this.patients = res.patients.data;
});

模板:

<ion-item *ngFor="let patient of patients">
       Name: {{patient.FirstName}}
</ion-item>