Json 错误:找不到不同的支持对象';[对象对象]';类型为';对象';。NgFor仅支持绑定到Iterables Angular 9
大家好,我有一个错误:找不到类型为“object”的不同支持对象“[object object]”。NgFor仅支持绑定到Iterables 我尝试了.json,但在angular 9中它不起作用 My component.htmlJson 错误:找不到不同的支持对象';[对象对象]';类型为';对象';。NgFor仅支持绑定到Iterables Angular 9,json,angular,observable,angular9,Json,Angular,Observable,Angular9,大家好,我有一个错误:找不到类型为“object”的不同支持对象“[object object]”。NgFor仅支持绑定到Iterables 我尝试了.json,但在angular 9中它不起作用 My component.html <div class="form-group"> <label for="">Ville</label> <sele
<div class="form-group">
<label for="">Ville</label>
<select class="form-control" #hh
(change)="getidregion(hh.value)">
<option></option>
<option *ngFor="let ville of dropDownList"
[value]="ville.id">
{{ville.nomVille}}
</option>
</select>
</div>
我的API数据:
dropDownList: [];
responsable = {
id: 0,
nomResponsable: '',
prenomResponsable: '',
emailResponsable: '',
mdpResponsable: '',
img: '',
rib: '',
dtn: '',
region: 0,
ville: 0,
};
methodAPIToGetVilles() {
this.res.getville()
.subscribe((s: any) => {
this.dropDownList = s;
});
}
//methode to get the value off the idville in the option
getidville(id: any): void {
this.responsable.ville = id;
}
curUser: any = this.responsable[0];
"@context": "/api/contexts/Ville",
"@id": "/api/villes",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/api/villes/6",
"@type": "Ville",
"nomVille": "Davidboeuf"
},
{
"@id": "/api/villes/7",
"@type": "Ville",
"nomVille": "Benoitboeuf"
}];
我想你是想做这样的事情:
<div *ngFor="let item of object | keyvalue">
{{item.key}}:{{item.value}}
</div>
{{item.key}}:{{item.value}
这个问题在这里也得到了很好的回答:
如果您使用的是*ngFor
,则它仅支持类型为数组或iterables的数据
在您的场景中,变量dropDownList中的数据要么为object类型,要么为null |未定义。因此会出现这种错误。在使用数据之前先对其进行控制台处理。您的API可能不会返回数组?是的,它返回一个对象,我不知道如何转换它。您可以发布它吗?还有一种方法可以使用keyvalue管道来循环对象。use console.log(this.dropDownList)并检查它显示的内容这是我的控制台日志:{@context:/api/contexts/Ville',@id:/api/villes',@type:“hydra:Collection”,hydra:member:Array(30),hydra:totalItems:403,}@context:/api/contexts/Ville“@id:/api/villes”@type:“hydra:Collection”hydra:member:Array(30)0:{@id:“/api/villes/6”,@type:“Ville”,nomVille:“Davidboeuf”}1:{@id:“/api/villes/7”,@type:“Ville”,nomVille:“Benoitboeuf”}
它返回一个对象,这就是问题所在!。它应该返回一个数组。我尝试了这个方法,结果是:{{dropDownList[ville].nomVille}