我正在尝试获取一组标签以在我的模板中正确打印标签。我有这个:
<div *ngFor="let tweet of tweets | async">
<p>
{{ tweet.msg}}
</p>
<p>
{{ tweet.username }}
</p>
Tags:
<div *ngFor="let tag of tweet.tags">
{{ tag }}
</div>
</div>
由于索引原因,我的firestore数据库是这样的:
tweets: {
msg: "joe",
username: "bill2",
tags: {
construction: true,
computers: true
}
}
除标签外,一切都正确打印。
我在想这样的事情,但后来我失败了。
this.tweetsCollection = this.afs.collection('tweets');
this.tweets = this.tweetsCollection.valueChanges();
this.tweets.subscribe(tags => {
tags.forEach(tag => {
Object.keys(tag).map(key => tag[key])
})
这显然行不通。那有必要吗?我想我想多了。
最佳答案
我想通了
this.tweetsCollection = this.afs.collection('tweets');
this.tweets = this.tweetsCollection.valueChanges()
.pipe(
tap(tweets => {
tweets.forEach(tweet => {
tweet['tags'] = tweet['tags'] ? tweet['tags'] : {}
let tags = [];
Object.keys(tweet['tags']).forEach(tag => {
tags.push(tag);
})
tweet['tags_list'] = tags;
})
})
)
这是几件事。我无法覆盖“tags”数组,我不得不创建一个新数组:“tags_list”。此外,如果没有 do() in angular < 5 或 pipe(tap()) in angular 6,您将无法在值更改后立即订阅。最后但同样重要的是,感谢 Jeremy W 和 Troy Myers,我现在捕获了 key 正确。它还异步更新标签,这很棒。谢谢大家。
关于javascript - angularfire2 - 获取标签数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51090296/