角度4-错误-找不到类型为“对象”的其他支持对象“ [对象对象]”

时间:2018-08-23 16:47:13

标签: angular ngfor

有一个来自HTTP的嵌套响应:

 this.dataSet = Observable.of({
     "insideData": [{
         id: "1"
         requiredData:[{
             firstname:"Amy"
             lastname: "Amy"
         }, {
             firstname:"John"
             lastname: "John"
        }]
    }]
 }}).map(mydata => mydata.insideData);

我将其包含在另一个Observable in组件中,并尝试将其减少为嵌套requiredData数组。

getItem(id) {    //id here is needed to get the first loop matching the id
    this.filteredData = this.dataSet.find(data => data.id ===id);
    this.gettingRequiredData = this.filteredData.map(data => data[0].json())
       .map(data => data.requiredData.json() || []);
}

在HTML中,我有一个ngFor循环,该循环正在迭代requiredDataSet

<div *ngFor="let data of gettingRequiredData; index as i;">......</div>

运行此代码时,它会给出

  ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

我不确定为什么选择对象而不是数组

1 个答案:

答案 0 :(得分:0)

绑定到可观察对象时,您忘记了使用异步管道。这是订阅Observable并发出值所必需的。

<div *ngFor="let data of gettingRequiredData | async; index as i;">......</div>