简体   繁体   English

错误:找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到Iterables

[英]Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables

I've been following an Ionic tutorial made by a content creator named Paul Halliday. 我一直在关注由内容创建者Paul Halliday制作的Ionic教程。 The project is a shopping list that uses Firebase and Angular. 该项目是使用Firebase和Angular的购物清单。

My problem is that whenever I run the app, I receive an error that looks like the following: 我的问题是,每当我运行该应用程序时,都会收到如下错误:

Error: Uncaught (in promise): Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

I've tried to look through other examples of how to fix my code, but none of them seem to comply with my code. 我试图查看其他有关如何修复代码的示例,但似乎都没有一个符合我的代码。

Currently, my page's Ts file is titled shopping.ts 目前,我页面的Ts文件名为shopping.ts

 import { Component } from '@angular/core'; import { NavParams, NavController } from 'ionic-angular'; import { AddShoppingPage } from '../addshopping/addshopping'; import { AngularFireDatabase} from 'angularfire2/database'; import { Observable } from 'rxjs/Observable'; import { ShoppingItem } from 'src/models/shopping-item/shopping-item.interface'; @Component({ selector: 'page-shopping', templateUrl: 'shopping.html' }) export class ShoppingPage { shoppingListRef$: Observable<ShoppingItem[]>; constructor(public navCtrl: NavController, public navParams: NavParams, private database: AngularFireDatabase ) { this.shoppingListRef$ = this.database.list('shopping-list').valueChanges(); } navigate2AddShoppingPg() { this.navCtrl.push(AddShoppingPage); } } 

There is also an interface that we have defined through a ts file: 我们还通过ts文件定义了一个接口:

export interface ShoppingItem {
itemName: string;
itemNumber: number;
}

Finally, the html of our shopping page is: 最后,我们购物页面的html是:

 <ion-header> <ion-navbar> <ion-title>Shopping List</ion-title> <ion-buttons end> <button (click)="navigate2AddShoppingPg()"> <ion-icon name="add"></ion-icon> </button> </ion-buttons> </ion-navbar> </ion-header> <ion-content padding> <ion-list> <ion-item *ngFor = "let item of shoppingListRef$"> <h2>Item Name: {{item.itemName}}</h2> <h3>Quantity: {{item.itemNumber}}</h3> </ion-item> </ion-list> </ion-content> 

Any help would be super appreciated. 任何帮助将不胜感激。 Thank you!! 谢谢!!

Edit 2 ~~three months after~~ I haven't visited this question in a while, but to avoid being misleading to those seeking a solution, yes, the async pipe is actually supported. 编辑~~三个月后~~我已经有一段时间没有访问这个问题了,但是为了避免误导那些寻求解决方案的人,是的,实际上支持异步管道。

Your shoppingListRef$ does not represent any iterable type for Angular. 您的shoppingListRef$不代表Angular的任何可迭代类型。 It's Observable<ShoppingItem[]> , whereas Observable by itself is uniterable. 它是Observable<ShoppingItem[]> ,而Observable本身是可合并的。 What you are trying to do is to iterate over value returned from the observable. 您想要做的是迭代从可观察对象返回的值。 Try something like below: 尝试如下所示:

/* controller field */
listItems: ShoppingItem[] = [];

this.database.list('shopping-list').valueChanges()
.subscribe((v: ShoppingItem[]) => {
  this.listItems = v.slice(0);
})

And in template: 并在模板中:

    <ion-item *ngFor = "let item of listItems">
        <h2>Item Name: {{item.itemName}}</h2>
        <h3>Quantity: {{item.itemNumber}}</h3>
    </ion-item>

And as mentioned by @user184994 you can as well use async on template, which basically under the hood will subscribe to observable and pass forward returned values. 就像@ user184994提到的那样,您还可以在模板上使用async ,基本上在幕后将订阅可观察的值并转发返回的值。
One remark, if you wish your view to keep in sync with data, this component change detection strategy can not be set to onPush 一句话,如果您希望视图与数据保持同步,则不能将此组件更改检测策略设置为onPush

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到可迭代对象,例如数组 - ERROR Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays Angular 7 找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Iterables,比如 Arrays - Angular 7 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays Angular 9 找不到支持“对象”类型的 object“[对象对象]”的不同。 NgFor 仅支持绑定到诸如 Arrays 之类的 Iterables - Angular 9 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays 找不到“object”类型的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables,例如 Arrays。 ? 怎么解决? - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. ? How solve it? 找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 仅支持绑定到可迭代对象,例如数组。 [Angular 8] - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.[Angular 8] 找不到支持 object 类型为“object”的“[object Object]”的差异。 NgFor 仅支持绑定到 Iterables,例如 Array。 json 数据 - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Array. json data 错误:“对象”类型的“[对象对象]”。 NgFor 仅支持绑定到 Iterables,例如 Arrays。 - 离子项目 - Error: '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. - Ionic Project 在角度6中找不到类型为“对象”的其他支持对象“ [对象对象]” - Cannot find a differ supporting object '[object Object]' of type 'object' in angular 6 错误:找不到支持 object '[object Object]' 类型 'object' 的差异 - Err: Cannot find a differ supporting object '[object Object]' of type 'object' 错误错误:在 Angular 项目上找不到支持类型为“object”的 object“[object Object]”的差异 - ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object' on Angular Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM