【问题标题】:Why are values in the console but not being displayed in the page?为什么控制台中有值但页面中没有显示?
【发布时间】:2020-05-28 10:56:52
【问题描述】:

值不显示在页面上,但显示在console.log中

//app.components.ts
    import { Component } from '@angular/core';
import {AngularFireDatabase } from 'angularfire2/database';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  posts: any[];
  constructor(db: AngularFireDatabase){
    db.list('posts').valueChanges().subscribe(posts => {  this.posts = posts; console.log(this.posts); });

  }
}

//app.component.html

    <ul>
  <li *ngFor="let post of posts">
{{post.$value}}
  </li>
</ul>

//火力基地

【问题讨论】:

  • 只做{{post}}

标签: javascript angular firebase firebase-realtime-database


【解决方案1】:

正如@Peter Haddad 所说,您必须在列表中添加{{post}} 而不是{{post.$value}},如下所示:

<ul>
  <li *ngFor="let post of posts">
{{post}}
  </li>
</ul>

更多信息: https://angular.io/guide/displaying-data#add-logic-to-loop-through-data

希望对你有帮助! :)

【讨论】:

    【解决方案2】:
    <ul>
      <li *ngFor="let post of posts">
    {{post}}
      </li>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 2017-04-09
      • 1970-01-01
      相关资源
      最近更新 更多