我尝试使用以下方法迭代一个数组*ngFor="let item of items"
,知道 ngFor 用于迭代几个数组的数组,如果我做一个返回我的查询,例如:学生注册的科目,知道学生可以有 1 或许多注册的职业。
如果注册了 1 个以上的科目,使用 ngFor 没有问题,但是如果学生只有 1 个学位注册,这就是发生在我身上的情况,ngFor 会生成一个错误,说它只能迭代多个数组的数组。
我该如何解决这个问题,因为正如我所说,这个查询以 json 格式返回我的数组,但它可以包含 1 个或多个元素。
如何使用 ngFor 遍历数组以便获得 1 个或多个元素?
如果我的数组是这样的,那么 ngFor 可以工作:
[
{
"userId": 1,
"title": "medicina",
"status": 1
},
{
"userId": 1,
"title": "informatica",
"status": 1
}
]
但是如果它是这样的,那么 ngFor 就不起作用了:
[
{
"userId": 1,
"title": "informatica",
"status": 1
}
]
它显示的错误如下:
error trying to diff. only arrays and iterables are allowed
我在控制台中显示了安排,如下所示:
这种安排是当学生只注册一个职业时
带来数据并将其放入我的 .ts 文件中的项目的方法如下
public items:any=[];
datosAcademicosUser() {
let data:Observable<any>;
data = this.http.getData(this.userDetails.id,"get/datauser");
data.subscribe( result => {
this.items = result;
}, (err) => {
console.log(err);
});
}
html代码如下
<ion-card-content *ngFor="let item of items">
<ion-grid>
<ion-row>
<ion-col col-12>
<button ion-button full block small round color="dark">{{item.NombCar}}</button>
</ion-col>
</ion-row>
</ion-grid>
<p *ngIf="item.status==1; else inactivo" style="color:crimson;font-weight: bold;text-align: center">
Tienes inscripción activa
</p>
<ng-template #inactivo>
<p style="color:crimson;font-weight: bold;text-align: center">Inactivo</p>
</ng-template>
</ion-card-content>
json格式的数组如下:
[
{
"id_inscripcion":"1",
"0":"1",
"estudiante_id":"1",
"1":"1",
"carrera_id":"192",
"2":"192",
"indice":"3.90",
"3":"3.90",
"status":"1",
"4":"1",
"turno":"Diurno",
"5":"Diurno",
"sede":"1",
"6":"1",
"condicion":"RG",
"7":"RG",
"uc_aprobadas":"150",
"8":"150",
"CodCar":"192",
"9":"192",
"NombCar":"INGENIERIA INFORMATICA",
"10":"INGENIERIA INFORMATICA",
"codVice":"1",
"11":"1",
"ciudad":"Barinas",
"12":"Barinas"
}
]
我正在使用角度 6
*ngFor仅适用于数组而不适用于对象。