[英]Angular Firestore Collection Snapshot Changes
我正在使用FireStore,我只需要將收集列表數據顯示到組件上,並在顯示的每個項目旁邊即可刪除該項目。
我需要使用SnapShotChanges()來獲取刪除方法的ID。
這是我的代碼。 沒有任何控制台被控制台記錄。 我正在使用Angular 7。
userCollection: AngularFirestoreCollection<any>;
collection: any;
constructor(private afs: AngularFirestore, private adminService: AdminServiceService) { }
ngOnInit() {
this.userCollection = this.afs.collection<any>('valuation');
this.collection = this.userCollection.snapshotChanges().pipe(
map(actions => {
actions.map(a => {
console.log(a.payload.doc.data);
console.log(a.payload.doc.id);
})
}
))
}
這是我的模板:
<tbody *ngFor="let o of collection | async">
<tr>
<td scope="row">
{{o.address}}
</td>
<td scope="row">
{{o.name}}
</td>
<td scope="row">
{{o.email}}
</td>
<td scope="row">
{{o.phone}}
</td>
<td scope="row">
<button mat-button color="warn" class="delete" type="button" (click)="delete(o)">Delete</button>
</td>
</tr>
</tbody>
當您使用snapshotChanges
AngularFirestore
會為您提供文檔以及該文檔的id
。
然后,您可以在action
上使用payload.doc.data()
提取文檔的實際值。
試試看:
import { Component } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
userCollection: AngularFirestoreCollection<any>;
collection: any;
constructor(private afs: AngularFirestore) { }
ngOnInit() {
this.userCollection = this.afs.collection<any>('valuation');
this.collection = this.userCollection.snapshotChanges()
.pipe(
map(actions => actions.map(a => a.payload.doc.data()))
);
}
}
這是供您參考的工作樣本StackBlitz 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.