【问题标题】:How to get a key of node in angularfirelist or angularfireobject in firebase using Ionic 3?如何使用 Ionic 3 获取 angularfirelist 或 firebase 中的 angularfireobject 中的节点键?
【发布时间】:2018-07-28 22:15:10
【问题描述】:

我正在尝试使用 AngularFireList 获取我的 firebase 数据节点密钥

Data.ts

getAddress(user){
this.AngularFireDatabase.list(`Profiles/Users/${user.uid}/address`);}



SendingData.ts



this.data.getAddress(user).valueChanges().subscribe(item=>{})

我得到了我在子节点中的所有东西,我怎样才能得到密钥?例如,在图像中,我有地址/家/,在家中我有一个名字,lat,lng。在这里,我想获得 Home,这是这些子节点的关键。

Firebase 数据图像:

有什么办法吗?

【问题讨论】:

    标签: typescript ionic2 ionic3 angular5


    【解决方案1】:

    如果你想获取密钥,你必须使用 snapshotChanges() 而不是 valueChanges() ,阅读Angularfire2 docs


    这是我的代码之一,看看它, 在 TS 文件中:

    ParkingSpotList$: Observable<any[]>; // write this as global variable before constructor 
     this.ParkingSpotList$ = this.AFD.ShowParkingSpot().snapshotChanges().map(changes => {
      console.log(changes)
      return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
    });
    

    然后在 HTML 中(我通过写“PS.key”来获取密钥):

      <ion-list>
        <ion-item  *ngFor="let PS of ParkingSpotList$ | async" (click)="ReserveParkingSpot(PS.key)">
          {{PS.PlateNo}}
        </ion-item>
      </ion-list>
    

    【讨论】:

      猜你喜欢
      • 2020-07-27
      • 2018-08-25
      • 2019-03-12
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2019-08-21
      • 1970-01-01
      相关资源
      最近更新 更多