Pat*_*emi 2 multidimensional-array angular 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
Run Code Online (Sandbox Code Playgroud)
我的服务
getProducts(): Observable<any> {
return this.http.get('http://localhost:8000/api/products')
.map(res => {
return res;
});
Run Code Online (Sandbox Code Playgroud)
我的组件
products: Products[];
constructor(private productservice: ProductService) { }
ngOnInit() {
this.productservice.getProducts()
.subscribe(res => this.products = res);
}
}
Run Code Online (Sandbox Code Playgroud)
当我在我的html文件中使用products属性进行for循环时,它将返回此错误。“错误:找不到类型为'object'的其他支持对象'[object Object]'。NgFor仅支持绑定到Iterables,例如数组。” 我需要纠正什么?以及如何正确显示产品对象?
编辑
<li class="span4" *ngFor="let product of products">
Run Code Online (Sandbox Code Playgroud)
那就是我的ngFor循环。
在您的服务中,您将返回一个仅具有一个属性data的对象,该属性仍然是对象,而不是数组。
尝试返回res.data:
getProducts(): Observable<Products[]> {
return this.http.get('http://localhost:8000/api/products')
.map(res => {
return res.data;
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7588 次 |
| 最近记录: |