【问题标题】:Angular 5 getting this error Cannot find a differ supporting object '[object Object]' of type 'object'Angular 5 收到此错误找不到类型为“object”的不同支持对象“[object Object]”
【发布时间】:2018-07-12 11:45:27
【问题描述】:

返回对象如下所示

data: { "23":
       { prop: abc, prop_2: def },
        "78":
       { prop: abc, prop_2: def },
       "2098":
       { prop: abc, prop_2: def },
      }

代码

<div *ngFor="let filterCar of data">
     // blah blah
  </div>

【问题讨论】:

  • 您正在尝试迭代 JSON 对象,而不是数组。如果你想使用 *ngFor,你应该将你的对象转换为一个对象数组。

标签: angular typescript


【解决方案1】:

您应该使用 Object.values 将您的对象转换为对象数组:

let tab = Object.values(data);

然后您可以使用 ngFor 对其进行迭代

<div *ngFor="let filterCar of tab">
 // blah blah
</div>

【讨论】:

    【解决方案2】:

    您不能在 Object 上调用 *ngFor,只能调用数组,您应该使用 Object.entries 将对象转换为对象数组:

    const arrayOfObject = Object.entries(data);
    

    然后您可以使用 *ngFor 迭代新变量

    <div *ngFor="let item of arrayOfObject "></div>
    

    MDN Object.entries()

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 2019-03-05
      • 2021-11-20
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多