简体   繁体   English

如何使用angular / typescript从Firebase获取数据并以html显示

[英]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.

相关问题 如何使用 Angular 显示从 typescript 文件到 html 文件的变量? - How to display a variable from a typescript file to a html file using Angular? 如何使用Angular将Typecode中的unicode显示为HTML - How to display unicode from Typescript into Html with Angular Angular 如何显示从打字稿到 HTML 的字符串 - Angular how to display a string from typescript to HTML 从 Angular 中的 Firebase 获取并显示数据 - Get and display data from Firebase in Angular 如何使用Firebase和Ionic 3以HTML显示数据 - How to display data in HTML using Firebase and Ionic 3 我们如何使用node.js从angular 7应用程序中的mysql数据库中获取数据并将其显示在angular组件html页面上 - How can we get data from mysql database in angular 7 application by using node.js and display it on angular component html page 如何使用Angular 7在html中显示JSON数据? - How to Display JSON Data in html using Angular 7? 如何在 2019 年从 angular firebase 获取数据? - How to get data from angular firebase in 2019? 如何使用 typescript 在 angular2 中获取设备显示的高度和宽度? - How to get height and width of device display in angular2 using typescript? 从Firebase使用Angular获取的数据不会立即显示 - Data fetched using Angular from Firebase does not immediately display
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM