[英]How to get data and display in html from firebase using angular/typescript
I want to display all my posts with description title and price in my html using angular i am using firebase . 我想在我的HTML中使用angular显示我的所有带有描述标题和价格的帖子,我正在使用firebase 。
Here is my Code 这是我的代码
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
<ion-title>
Medical Tourism
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
{{title}}
{{desc}}
{{price}}
</ion-content>
<ion-tabs>
<ion-tab label="Get Card" icon="information-circle" href="tabs/(get-card:get-card)">
<ion-router-outlet stack name="get-card"></ion-router-outlet>
</ion-tab>
</ion-tabs>
When I console.log() I get all my posts whit all I need but when I get it on html there is just first post with description title and price. 当我console.log()我得到所有我需要的帖子时,但是当我在html上获得它时,只有第一篇文章带有描述标题和价格。
But in Console i get it 但是在控制台中我明白了
import { Component, OnInit, ViewChild } from '@angular/core';
import * as firebase from 'firebase/app';
import { AngularFireDatabase } from 'angularfire2/database';
@Component({
selector: 'app-medtour',
templateUrl: './medtour.page.html',
styleUrls: ['./medtour.page.scss'],
})
export class MedtourPage implements OnInit {
title: any;
desc: any;
price: any;
constructor(public db: AngularFireDatabase) {
firebase.database().ref().child("medtour_posts/").on('value', (shapshot) => {
shapshot.forEach((child) => {
this.title = child.val().title;
this.desc = child.val().description;
this.price = child.val().price;
console.log(this.title,this.desc,this.price);
})
})
}
ngOnInit() {}
}
well your console.log is inside the for each loop thats why it gets printed everytime. 好的,您的console.log位于for每个循环中,这就是为什么每次都打印它的原因。 But you're assigning the value to the same variable each time.
但是,您每次都将值分配给相同的变量。 it gets overwritten.
它会被覆盖。 You'll need an array or multiple variables to display all
您需要一个数组或多个变量才能显示全部
export class MedtourPage implements OnInit {
childs: [];
constructor(public db: AngularFireDatabase) {
firebase.database().ref().child("medtour_posts/").on('value', (shapshot) => {
shapshot.forEach((child) => {
this.childs.push(
{title:child.val().title),
desc:child.val().description),
price:child.val().price)};
})
})
}
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
<ion-title>
Medical Tourism
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding *ngFor="let child in childs">
{{child.title}}
{{child.desc}}
{{child.price}}
</ion-content>
<ion-tabs>
<ion-tab label="Get Card" icon="information-circle" href="tabs/(get-card:get-card)">
<ion-router-outlet stack name="get-card"></ion-router-outlet>
</ion-tab>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.