【问题标题】:AngularFire2 RealTime DataBase sort by date valueAngularFire2实时数据库按日期值排序
【发布时间】:2018-07-02 17:45:15
【问题描述】:

我的服务

get ActivityList():any {
    return this.activityList.snapshotChanges().map(changes => {
        return changes.map(c => ({ 
            key: c.payload.key, ...c.payload.val();
        }));
    });
}

我的组件

getActivity() {
    this.activityService.ActivityList.subscribe(activity => {
        this.activity = activity;
    });
}

HTML

<div *ngFor="let item of activity">
    {{item.created|date:'medium'}}
</div>

问题

这就是我目前从 Firebase 实时数据库获取活动的方式。在哪里以及如何修改它以按created 值排序的最佳方法。

【问题讨论】:

    标签: javascript angular firebase firebase-realtime-database angularfire


    【解决方案1】:

    将服务更改为

    get ActivityList():any {
        return this.activityList.snapshotChanges().map(changes => {
            return changes.map(c => ({ key: c.payload.key, ...c.payload.val() })).sort((a, b) => b.created - a.created );
        });
    }
    

    【讨论】:

      【解决方案2】:

      订阅后排序。

      getActivity() {
          this.activityService.ActivityList.subscribe(activity => {
              this.activity = activity.sort((a, b) => a.create - b.create );;
          });
      }
      

      【讨论】:

      • 现在正在解决这个问题changes.map(...).do is not a function
      • 这不起作用。我正在尝试不同的变体。
      • 只需添加我的 2 美分,添加 sachila 在管道中编写的那种类型,以便在其他地方使用该功能,而无需添加更多代码。
      • ERROR TypeError: list.sort is not a function .... 也在尝试管道
      • grrr, do 运算符应该可以工作。好的,我想你必须在订阅中对其进行排序,检查更新
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 2019-01-17
      • 2016-03-25
      相关资源
      最近更新 更多