【问题标题】:Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays with Async pipe找不到“object”类型的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables,例如带有异步管道的数组
【发布时间】:2019-07-27 05:28:55
【问题描述】:

.html

 <ion-select-option value="t.result[i].id" *ngFor="let t of (issueTypes$ | async); let i = index ">
            {{t.result[i].name}}
 </ion-select-option>

.ts

this.issueTypes$ = this.maintenanceService.get();

邮递员:

{
    "result": [
        {
            "id": "KT5c6wdb8ecd94e",
            "name": "Need Batteries"
        },
        {
            "id": "RT5c6aa12600134",
            "name": "A/C Not working"
        }
    ]
}

错误错误:找不到不同的支持对象“[object Object]” '对象'类型。 NgFor 仅支持绑定到 Iterables,例如 数组。 在 NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck (common.js:3161) 在 checkAndUpdateDirectiveInline (core.js:22004) 在 checkAndUpdateNodeInline (core.js:23265) 在 checkAndUpdateNode (core.js:23227) 在 debugCheckAndUpdateNode (core.js:23861) 在 debugCheckDirectivesFn (core.js:23821) 在 Object.eval [as updateDirectives] (MaintenancePage.html:19) 在 Object.debugUpdateDirectives [as updateDirectives] (core.js:23813) 在 checkAndUpdateView (core.js:23209) 在 callViewAction (core.js:23450)

你能告诉我这里的问题吗?

【问题讨论】:

  • 您可能在返回的类型上指的是resultlet t of (issueTypes$ | async)?.result; let i = index

标签: angular typescript rxjs angular7 ionic4


【解决方案1】:

根据 postman 的 json 模型,您应该迭代 result 属性,该属性是返回模型中的数组。

*ngFor="let t of (issueTypes$ | async)?.result; let i = index"

那么在包含的html里面就可以直接访问t里面包含了数组中的对象。索引变量i 也不再需要了。

{{t.name}}

【讨论】:

  • 哇...真不错。谢谢。
【解决方案2】:

问题是您试图迭代结果对象。你需要更深一层。因此,将您的代码更改为以下内容:

<ion-select-option value="t.id" *ngFor="let t of (issueTypes$ | async)?.result; let i = index ">
            {{t.name}}
 </ion-select-option>

【讨论】:

  • 因为issueTypes$ 是解决.result 的可观察/承诺,在它解决后应该调用它。 (issueTypes$ | async)?.result
  • 好点。老实说,我不会以这种方式进行 ajax 调用,但我会更新我的答案。
猜你喜欢
  • 1970-01-01
  • 2017-02-10
  • 2020-05-10
  • 2018-10-29
  • 2019-04-04
  • 2020-10-17
  • 2017-10-15
  • 2019-05-12
相关资源
最近更新 更多