我是ionic/angular中的一个noob,我试图从JSON中检索一些数据,并通过cards((将其显示在HTML中
JSON包含许多对象;deTurno==true";或";deTurno==false";
到目前为止,我有这个:
public data: any;
public test: any;
public testTwo: any;
constructor(private apiService: ApiService) {
this.apiService.getFarmacias().subscribe(
(data) => {
this.data = data;
for (const item of this.data) {
if (item.deTurno == true) {
// copy the item inside this.test
} else {
// copy the item inside this.testTwo
}
}
}
);
}
我想要的是获得JSON中与";deTurno==true";,并将它们放入测试中,这样我就可以使用测试在HTML中显示这些项目,如下所示:
<ion-card *ngFor="let dataItem of test">
<ion-card-header>
<ion-card-subtitle>{{dataItem.direccion}}</ion-card-subtitle>
<ion-card-title>{{dataItem.nombre}} (De turno hoy)</ion-card-title>
<h5>Localidad: {{dataItem.localidad}}</h5>
<h4>{{dataItem.telefono1}}</h4>
</ion-card-header>
</ion-card>
我只需要显示那些与";deTurno==true";,然后用与"匹配"的项目做一些事情;deTurno==false";,否则,我将只显示";数据";
请帮助:(
您的问题是
public test: any;
你们并没有定义测试,所以当你们试图推动它时,它是未定义的。
public data: any[] = [];
public test: any[] = [];
public testTwo: any[] = [];
constructor(private apiService: ApiService) {
this.apiService.getFarmacias().subscribe(
(data) => {
this.data = data;
for (const item of this.data) {
if (item.deTurno == true) {
// copy the item inside this.test
this.test.push(item);
} else {
// copy the item inside this.testTwo
this.testTwo.push(item);
}
}
}
);
}