【发布时间】:2018-12-29 13:04:32
【问题描述】:
我正在尝试从 ionic3 中的 FIREBASE 获取数据并在页面中打印所有数据。我得到数据,我把它登录到控制台,一切都很好,我看到了结果。但它是作为一个对象。我想通过 {{object.data}} 从该对象获取特定数据,其中 'object' = 对象的名称和 'data' = 我想要从该对象获得的内容。但它没有给出任何输出,而是给出了错误。
对不起,我是新来的,请在提供的图片中查看我的代码。谢谢你。
这是我的 home.ts 文件
import { Component } from '@angular/core';
import { NavController, IonicPage, NavParams } from 'ionic-angular';
import { ShoppingListService } from '../../services/shopping-list.service';
import { Item } from '../../models/items/item.interface';
import {Observable} from 'rxjs/Observable';
import { AngularFireDatabase } from 'angularfire2/database';
@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
shoppingList$: Observable<Item[]>;
items: any;
constructor(private shopping: ShoppingListService,private afDb: AngularFireDatabase,
private navCtrl: NavController, private navParams: NavParams) {
//A problem here with functions or with home.html PIPE
//I cannot display values of shoppingList$
/*this.shoppingList$ = this.shopping.getShoppingList()
.snapshotChanges()
.map(changes=>{
return changes.map(c=>({
key: c.payload.key, ...c.payload.val()
}))
}) */
//let key = this.navParams.get('key');
this.afDb.object(`shopping-list`).valueChanges().subscribe(data=>{
console.log(data);
this.items = data;
})
}
}
这是我的家.html
<ion-content padding>
<ion-list>
<h2>Items</h2>
<ion-item *ngFor="let item of items">
{{item.name}}
</ion-item>
</ion-list>
</ion-content>
【问题讨论】:
-
你的 console.log(data) 显示了什么?
-
@Sajeetharan Object { name: "huawei", price: "2300", quantity: "3" } 之后出现这个错误:“Cannot find a different support object '[object Object]' of type 'huawei'。NgFor 只支持绑定到 Arrays 等 Iterables。"
标签: angular typescript firebase firebase-realtime-database ionic3