Angular:ng-repeat对象的逆序

时间:2016-08-25 16:13:23

标签: javascript angularjs

过滤

app.filter('orderObjectBy', function() {
  return function(items, field, reverse) {
    var filtered = [];
    angular.forEach(items, function(item) {
      filtered.push(item);
    });
    filtered.sort(function (a, b) {
      return (a[field] > b[field] ? 1 : -1);
    });
    if(reverse) filtered.reverse();
    return filtered;
  };
});

Data JSON:

{
  "results": {
    "100": {
      "numbers": {
        "1": {
          "key": "value"
        },
        "2": {
          "key": "value"
        },
        "3": {
          "key": "value"
        }
      }
    },
    "200": {
      "numbers": {
        "1": {
          "key": "value"
        },
        "2": {
          "key": "value"
        },
        "3": {
          "key": "value"
        }
      }
    }
  }
}

渲染:

<div ng-repeat="(k,v) in results">
  <span ng-repeat="(k1,v1) in v | orderObjectBy:k1:true">{{k1}}</span>
</div>

orderObjectBy:k1:true不返回“密钥”,而是返回密钥的数组,例如0 1 2。我该如何解决并扭转订单?

2 个答案:

答案 0 :(得分:2)

您可以尝试使用此

ng-repeat='item in items| orderBy:item.key:reverse'

或可能是

ng-repeat='item in items.reverse()'

答案 1 :(得分:0)

在我的自行车CMS(自行车是obj)中,我想颠倒自行车清单

这就是我逆转的方式:

<tr ng-repeat="bike in bikes | orderBy: '-' ">

我希望能帮助某人:D