简体   繁体   English

AngularFire-如何在集合查询中显示Firestore键和值

[英]AngularFire - How to display Firestore key and value in collection query

I'm using a standard AngularFire collection query. 我正在使用标准的AngularFire集合查询。 However a project requirement has dictated that instead of explicit binding firestore nodes and values directly in the HTML (as seen in the first HTML snippet below), that we instead bind the returned key and value so that if any new fields are added in Firestore, I won't need to update my project to display those items. 但是,根据项目要求,我们不是直接在HTML中显式绑定Firestore节点和值(如下面的第一个HTML代码段所示),而是绑定返回的键和值,以便在Firestore中添加任何新字段时,我不需要更新项目即可显示这些项目。

Is there a way, with the standard AngularFire query to bind the key and value of each returned result in the HTML? 有没有办法使用标准AngularFire查询将HTML中每个返回结果的键和值绑定在一起?

user.component.ts user.component.ts

  getUsers(){
    this.usersCollection = this.afs.collection<User>('users');
    this.users = this.usersCollection.snapshotChanges().pipe(
      map(actions => actions.map(a => {
        const data = a.payload.doc.data() as User;
        const id = a.payload.doc.id;  
        return { id, ...data };
      }))
    );
  }

HTML HTML

<ul>
    <li *ngFor="let user of users | async">
        <div>{{ user.name }}</div>
        <div>{{ user.dob }}</div>
        <div>{{ user.id }}</div>
    </li>
</ul>

Desired Result 所需结果

<ul>
    <li *ngFor="">
        <div>{{key}} / {{ value }}</div>
    </li>
</ul>

There is a pipe called | keyvalue 有一个叫| keyvalue的管道| keyvalue | keyvalue (angular 6+). | keyvalue (角度为6+)。 So what you can do is: 因此,您可以做的是:

<ul>
    <li *ngFor="let user of users | async">
        <div *ngFor="let userinfo of user | keyvalue">
          {{ userinfo.key}}: {{userinfo.value}}
        </div>
    </li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AngularFire-将来自Firestore集合查询的数据推入数组 - AngularFire - Push data from firestore collection query into an array 如何在 angularfire firestore 中删除整个文档集合 - How to do remove an entire document collection in angularfire firestore 如何使用angularfire从另一个集合中获取firestore中的用户名 - how to get a username in firestore from another collection with angularfire 使用Firestore / Angularfire2或Firestore SDK查询集合中的内部对象ID - Query inner object id in a collection using Firestore/ Angularfire2 or Firestore SDK 如何格式化和显示从 AngularFire 返回的 Firestore 数据? - How can I format and display Firestore data returned from AngularFire? 使用 Angularfire2 和 firestore 加入查询 - Join query with Angularfire2 and firestore 如何获取angularfire2中特定键的值? - How to fetch value for a particular key in angularfire2? Angular / AngularFire2 Firestore-初始化可观察的集合 - Angular / AngularFire2 Firestore - Initialise Observable Collection 如何将一个集合作为子集合复制到另一个集合中? Angularfire/ Angular/ Firestore - How do I copy a collection into another, as a sub collection? Angularfire/ Angular/ Firestore Angularfire展示关键和价值 - Angularfire show key and value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM