【问题标题】:How to sort ng-repeat key value pair getting from firebase如何对从 firebase 获取的 ng-repeat 键值对进行排序
【发布时间】:2018-09-13 13:13:16
【问题描述】:

我是 angularJS 的新手,我从 firebase 获取键值对如何按时间戳排序以显示最新的顶部。 请帮我 。在此先感谢,orderBy 和 reverse 不起作用

var ref = new Firebase("https://firebaseio.com/notifications/");
var fb = $firebase(ref);

var syncArreglo = fb.$asObject();

syncArreglo.$loaded().then(function() {
    angular.forEach(syncArreglo, function (value, key) {
        if(syncArreglo.value != null){
            syncArreglo.value.firebaseKey = syncArreglo.key;
        }
    });
});

syncArreglo.$bindTo($scope,'chats');

<ul class="menu" ng-repeat="(key , value) in chats">

【问题讨论】:

  • 提供到目前为止您在获取键值对方面所做的代码
  • 请检查代码

标签: angularjs firebase angularjs-ng-repeat


【解决方案1】:

假设你有图表 json 为:

$scope.chats = [
        {item: 'a',  created : '2016-12-01', num :1},
        {item: 'b',  created : '2015-12-01' , num :4},
        {item: 'c',  created : '2018-12-01', num :5},
    ];

要对项目进行排序,您需要提供过滤器orderBy : sortItem : true,如

<ul ng-repeat="(key, value) in chats | orderBy : sortItem : true">
            <li>{{value}} :: {{value.created}}</li>
        </ul>

您的控制器将具有 sortitem 函数,它将告诉排序项目函数

控制器

 $scope.sortItem = function(d) {
      var dateing = new Date(d.created); 
      return dateing;  
  };

玩转 jsfiddle:http://jsfiddle.net/agjqN/688/

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2016-04-15
  • 2013-02-03
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多