[英]How to retrieve single product details from firebase realtime database in angular 9 and print it in html?
product.component.ts product.component.ts
import { AngularFireDatabase } from '@angular/fire/database';
import { ProductService } from './../product.service';
import { ActivatedRoute } from '@angular/router';
import { Component, OnInit} from '@angular/core';
@Component({
selector: 'app-product',
providers:[],
templateUrl: './product.component.html',
styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {
product;
object9;
constructor(public route: ActivatedRoute,public db: AngularFireDatabase) {
this.id= this.route.snapshot.paramMap.get('id');
console.log(this.id);
this.object9=this.db.object('products/'+this.id).valueChanges().subscribe(val =>{
console.log(val);
});
}
ngOnInit() {
}
}
product.component.html
{{ object9.fullName|async|json}}
Firebase realtime Database: Firebase 实时数据库:
oshop-1df92
products
-M9vLP-mF2DAIMkkKVR_
email:
fullName:
-M9vLfD2r3PrQbXQiYot
-M9vSjV9lNVZ2QhIj63n
In console I am getting id printed and console.log(val) prints object having the id,but in html it is printing null.Why??在控制台中,我打印了 id,console.log(val) 打印了具有 id 的 object,但在 html 中打印的是 null。为什么? I am using latest angular 9 with latest version of firebase and angularfire.Oshop is name of project.我正在使用最新的 angular 9 和最新版本的 firebase 和 angularfire.Oshop 是项目的名称。 Please help me out.请帮帮我。
By assigning the subscription to the variable object9 and not the data that you get from the subscription.通过将订阅分配给变量object9而不是您从订阅中获得的数据。 Instead replace your code of assinging the value by the following lines:而是用以下几行替换您分配值的代码:
this.db.object('products/'+this.id).valueChanges().subscribe(val =>{
console.log(val);
this.object9= val
});
}
And now simply print the value in html using:现在只需使用以下命令打印 html 中的值:
{{object9.fullname}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.