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');
}
答案 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>