【发布时间】:2019-12-03 17:36:46
【问题描述】:
CategoryComponent.html:57 ERROR 错误:找不到类型为“Samsung”的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables,例如 Arrays。 我只能显示产品数组休息它不适用于此代码
这是我在控制台中遇到的错误
<mat-accordion multi="true" *ngFor="let item1 of products">
<mat-expansion-panel *ngFor="let item2 of item1.childcat">
<mat-expansion-panel-header *ngFor="let item3 of item2.subcat">
{{item2.name}}{{ item3.name}}
</mat-expansion-panel-header>
<p>HAlo</p>
</mat-expansion-panel>
</mat-accordion> <!-- filter-group .// -->
我的组件
this.route.queryParams.subscribe(params => {
this.categoryService.getProduct().subscribe(products => this.products = products)
服务文件
getProduct(): Observable<Product[]>{
let params_url = new HttpParams().set('fil_outlet',this.name);
console.log('new my ss--',this.name);
return this.http.get<Product[]>("http://localhost:4500/products",{params:params_url});
};
JSON
[
{
"id": 1,
"name": "A7",
"childcatId": "1",
"subcatId": "1",
"price": 18000,
"mrp": 21000,
"discription": "sfhshsfhsf",
"image": "http://bestjquery.com/tutorial/product-grid/demo3/images/img-7.jpeg",
"createdAt": "2019-11-13T00:00:00.000Z",
"updatedAt": "2019-11-14T02:00:00.000Z",
"childcat": {
"id": 1,
"name": "samsung",
"categoryId": "1",
"subcatId": "1",
"createdAt": "2019-11-21T00:00:00.000Z",
"updatedAt": "2019-11-21T00:00:00.000Z",
"subcat": {
"id": 1,
"name": "Mobile",
"categoryId": "1",
"createdAt": "2019-11-19T00:00:00.000Z",
"updatedAt": "2019-11-05T00:00:00.000Z",
"filter_groups": [
{
"id": 1,
"name": "Ram",
"subcatId": "1",
"createdAt": "2019-11-13T00:00:00.000Z",
"updatedAt": "2019-11-13T00:00:00.000Z",
"filters": [
{
"id": 1,
"name": "2 GB",
"filterGroupId": "1",
"productId": "1",
"createdAt": "2019-11-19T00:00:00.000Z",
"updatedAt": "2019-11-27T00:00:00.000Z"
}
]
},
{
"id": 2,
"name": "Internal Storage",
"subcatId": "1",
"createdAt": "2019-11-05T00:00:00.000Z",
"updatedAt": "2019-11-24T00:00:00.000Z",
"filters": [
{
"id": 2,
"name": "8 GB",
"filterGroupId": "2",
"productId": "1",
"createdAt": "2019-11-20T00:00:00.000Z",
"updatedAt": "2019-11-20T00:00:00.000Z"
}
]
}
]
}
}
}
]
【问题讨论】:
-
您正在尝试迭代 item1.childcat。而且 childcat 不是一个数组,而是一个对象。因此错误。有一个 childcat,你为什么要迭代它?另外,为什么返回一个名为 getProduct() 的 products 数组的方法。正确使用单数和复数形式会使事情变得更容易。将变量命名为
product而不是item1也会使事情变得更容易。
标签: angular