Angular 5 找不到类型为 '[object Object]' 的不同支持对象 'object' 。 NgFor 仅支持绑定(bind)到 Iterables,例如数组

标签 angular multidimensional-array angular5

我正在尝试从我的后端 API 返回产品并将其显示在我的前端页面上。当我执行 *ngFor 循环时,它给了我一个错误。这是我的代码。

我的后端 API

data    
   0    
    name    "perferendis"
    totalPrice  323.76
    rating  5
    discount    43
  href  
    link    "http://localhost:8000/api/products/1"
   1    
     name   "non"
     totalPrice 92.34
     rating 3.5
     discount   19
   href 
      link  "http://localhost:8000/api/products/2"
    2   
     name   "a"
     totalPrice 246.76
     rating 3
     discount   38
  href  
    "http://localhost:8000/api/products/3"
    3   
     name   "vitae"
     totalPrice 537.57
     rating "No rating yet"
     discount   1
   href 
     link   "http://localhost:8000/api/products/4"

  links 
     first  "http://localhost:8000/api/products?page=1"
     last   "http://localhost:8000/api/products?page=3"
     prev   null
     next   "http://localhost:8000/api/products?page=2"
meta    
   current_page 1
from    1
last_page   3
path    "http://localhost:8000/api/products"
per_page    20
to  20
total   60

我的服务

getProducts(): Observable<any> {
  return this.http.get('http://localhost:8000/api/products')
  .map(res => {
      return res;
  });

我的组件

products: Products[];

constructor(private productservice: ProductService) { }

  ngOnInit() {
  this.productservice.getProducts()
  .subscribe(res => this.products = res);
  }

}

当我在 html 文件中使用 products 属性执行 for 循环时,它会返回此错误。 “错误:找不到类型为‘object’的不同支持对象‘[object Object]’。NgFor 仅支持绑定(bind)到可迭代对象,例如数组。”我需要纠正什么?如何正确显示产品对象?

编辑

<li class="span4" *ngFor="let product of products">

这是我的 ngFor 循环。

最佳答案

在您的服务中,您将返回一个仅具有一个属性data的对象,该对象仍然是一个对象,而不是一个数组。

尝试返回res.data:

getProducts(): Observable<Products[]> {
  return this.http.get('http://localhost:8000/api/products')
  .map(res => {
      return res.data;
  })

关于Angular 5 找不到类型为 '[object Object]' 的不同支持对象 'object' 。 NgFor 仅支持绑定(bind)到 Iterables,例如数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49084415/

相关文章:

Angular 6 : Dynamic Meta tag is not working when sharing page on facebook using og:description tag

javascript - 使用 Material Sidenav 登录和注销路由

java - JAVA取N个数组并将其转换为具有N行的多维数组

PHP二维数组转HTML输入值

android - 没有虚拟方法 getActiveNotifications()

html - 单击 Ionic 3 展开标题

javascript - 响应式(Reactive)表单 Angular 格式输入数据

angular - Jhipster 启动速度非常慢 - 启动空白 Bootstrap 应用程序需要 2 小时

javascript - 如何将 Amazon AWS 的客户端 javascript 导入我的 Ionic 2/Angular 2 应用程序?

mongodb - 嵌套数组插入mongodb?