问题描述
尝试从Firebase数据库中检索数据和键值,但仅数据有效。我试图返回数据和键作为一个对象,但是它不起作用。有人可以帮忙弄清楚如何在html标记的url路径中同时呈现数据和键值吗?
**服务文件
import { AngularFireDatabase } from '@angular/fire/database';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ProductService {
constructor(private db: AngularFireDatabase) { }
// insert a new product into product obejct
create(product) {
return this.db.list('/products').push(product);
}
// get all products from db
getAllProducts() {
return this.db.list('/products').snapshotChanges()
.pipe(map( action => action
.map(a => {
const key = a.payload.key;
const data = a.payload.val();
return data;
})));
}
}
** TS文件
import { ProductService } from './../../../services/product.service';
import { Component,OnInit } from '@angular/core';
@Component({
selector: 'app-admin-products',templateUrl: './admin-products.component.html',styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent {
// Observable
products$;
constructor(private productService: ProductService) {
this.products$ = productService.getAllProducts();
}
}
* HTML标记
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let p of products$ | async">
<td>{{ p.title }}</td>
<td>{{ p.price | currency:'USD':true}}</td>
<td>
<a [routerLink]="['/admin/products',p.key]">Edit</a>
</td>
</tr>
</tbody>
</table>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)