我有返回给我的数据,工作正常
工作数据是
data: Array(16)
不工作的数据是这样的
data: Menu1Items: Array(5) > 0 { .... } etc
我使用的是 Angular 5,所以服务会返回这样的数据
.map((response: Response) => {
return response;
然后组件拦截它并且 console.log 工作正常
this.arsSevice.getMenu()
.subscribe(
result => {
this.testing = result;
console.log('menu',result);
})
问题出在数据上,这个截图显示了问题,我只是不明白如何用对象和数组来修复它?
错误信息只是因为 HTML 模板
Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
HTML 模板
<li *ngFor="let item of testing">
图像显示在体系结构中与 html 模板循环、组件、服务工作调用类似的调用是 BOTTOM,component
注意与名为 menu
的不同之处我遇到了麻烦?
最佳答案
我认为您需要设置 testing = result.data,并对其进行迭代。
this.arsSevice.getMenu()
.subscribe(
result => {
this.testing = result.data;
})
这将使您可以访问“数据”中的数组
我试图改变数据的形状,这对我有用。希望它对你有用......
var data={
menu1Items:[{key:"boo", key2:"hoo"}],
menu2Items:[{key:"boo2", key2:"hoo2"}]
}
var tempData:any[]=[];
for(var key in data){
if(data.hasOwnProperty(key)){
tempData.push(data[key]);
}
}
this.data = tempData;
}
在你的模板中:
<ul *ngFor="let menu of data ">
<li>
<ng-container *ngFor="let menuItem of menu">
{{menuItem.key}} / {{ menuItem.key2}}
</ng-container>
</li>
</ul>
关于javascript - 通过服务从 api 调用返回到组件的数据是一个对象,似乎需要成为 Angular 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47644549/