【问题标题】:Error - NgFor only supports binding to Iterables such as Arrays错误 - NgFor 仅支持绑定到 Iterables,例如 Arrays
【发布时间】:2021-07-06 14:42:11
【问题描述】:

我正在尝试将数组显示为下拉菜单中的选项,但出现错误:

错误错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。

HTML 代码:

<mat-option *ngFor="let type of exportTemplates" value="{{ type.id }}">
    {{ type.label }}
 </mat-option>

数组看起来像这样:

TS 代码:

exportTemplates = [];
const subs = this.configService.state$.subscribe((config: any) => {
            this.exportTemplates = config.templates.types;
        });
        this.subscription.push(subs);

【问题讨论】:

  • 那不是数组。

标签: arrays angular typescript


【解决方案1】:

如何合并数组中的所有项目

list.map(item => Object.keys(item).map(key => item[key]))[0]

已创建 Stackblitz Demo 供您参考

【讨论】:

    猜你喜欢
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多