Angular fire : 从文档中的映射中获取键值作为可观察值
Angular fire : get the keys,values from map in a document as an observable
这是我的 firestore 文档。我想获取可观察的评论列表,订阅它并遍历评论。如何使用 angular fire 做到这一点?
我是这里的初学者,所以如果你能详细解释一下,我将非常感谢你
谢谢!
您可以做的是为文档创建一个可观察对象,在发生更改时发出文档数据。
然后订阅这个 observable 来获取数据并在你的 html 中显示你想要的数据。
这是一个可以改进的简单示例。
app.component.ts
import {Component} from '@angular/core';
import {AngularFirestore} from '@angular/fire/firestore';
import {Observable} from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
doc$: Observable<any>;
collection = 'Agriculture_and_cropes';
docId = 'Replace by your document Id';
constructor(db: AngularFirestore) {
this.doc$ = db.collection(this.collection).doc(this.docId).valueChanges();
}
}
app.component.html
<ul *ngIf="doc$ | async as doc">
<li *ngFor="let comment of doc.comments">
{{comment.name}} : {{comment.desc}}
</li>
</ul>
在你的 component.ts 文件上
doc: issue;
doc_comments: Array<string>;
ngOnInit() {
db.collection('Agriculture_and_cropes').doc<issue>('doc_Id').valueChanges().subscribe(data => {
this.doc = data;
this.doc_comments = Array.from( data.comments.keys() );
});
}
在你的component.html
<ul *ngIf="doc_comments">
<li *ngFor="let c of doc_comments">
{{c}} : {{doc.comments[c]}}
</li>
</ul>
这是我的 firestore 文档。我想获取可观察的评论列表,订阅它并遍历评论。如何使用 angular fire 做到这一点? 我是这里的初学者,所以如果你能详细解释一下,我将非常感谢你
谢谢!
您可以做的是为文档创建一个可观察对象,在发生更改时发出文档数据。 然后订阅这个 observable 来获取数据并在你的 html 中显示你想要的数据。 这是一个可以改进的简单示例。
app.component.ts
import {Component} from '@angular/core';
import {AngularFirestore} from '@angular/fire/firestore';
import {Observable} from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
doc$: Observable<any>;
collection = 'Agriculture_and_cropes';
docId = 'Replace by your document Id';
constructor(db: AngularFirestore) {
this.doc$ = db.collection(this.collection).doc(this.docId).valueChanges();
}
}
app.component.html
<ul *ngIf="doc$ | async as doc">
<li *ngFor="let comment of doc.comments">
{{comment.name}} : {{comment.desc}}
</li>
</ul>
在你的 component.ts 文件上
doc: issue;
doc_comments: Array<string>;
ngOnInit() {
db.collection('Agriculture_and_cropes').doc<issue>('doc_Id').valueChanges().subscribe(data => {
this.doc = data;
this.doc_comments = Array.from( data.comments.keys() );
});
}
在你的component.html
<ul *ngIf="doc_comments">
<li *ngFor="let c of doc_comments">
{{c}} : {{doc.comments[c]}}
</li>
</ul>