【问题标题】:Data interpolation not working in Angular html template数据插值在 Angular html 模板中不起作用
【发布时间】:2022-01-13 22:46:19
【问题描述】:

我有一个从实时数据库读取数据的服务
来自product.service.ts

getAll(){
    // Get All Products
    return this.db.list("/products").snapshotChanges();
  }

我用来读取products.component.ts 中的数据:

products$:Observable<any>;
  
  constructor(productService : ProductService) {
    this.products$ = productService.getAll();
  }

但如果我尝试插入模板中的值,它将无法正常工作。正在做

<div *ngFor = "let p of products$ | async">
    <div>
        title: {{p.title}}
    </div>
</div>

将呈现: "title:" ,没有插入实际值。 可能是什么问题?

如果我查看完整的对象 p,甚至 p.payload,我可以看到数据已加载且正确,但它不会显示

{{ p | json}}

{{ p.payload | json }}

两者都按预期工作

【问题讨论】:

  • 当你说 p 和 p.payload 都按预期工作时,你看到了什么?
  • 请分享p | json的实际输出以及所需的输出。

标签: html angular firebase-realtime-database angularfire


【解决方案1】:

试试

constructor(productService : ProductService) {
    this.products$ = productService.getAll()
        .pipe(map( action => action
          .map(a => {
            return a.payload.val();
          })));
}

【讨论】:

  • 想详细说明您的key 变量的用途?
  • 最终选择了这种方法,因为我最终需要在显示之前处理数据。谢谢哈姆扎!
猜你喜欢
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 2015-11-29
相关资源
最近更新 更多