在ng-repeat

时间:2016-02-17 14:40:12

标签: javascript html angularjs

我有一个名为onlineInClubsCount的对象

{
    name: 12,
    anotherName: 2,
    anotherOne: 74
}

我遍历ng-repeat,如:

<a ng-repeat="(club,value) in onlineInClubsCount" class="item item-avatar" ng-href="#/app/clubs/{{club}}">
    <img ng-src="img/tc-logo.jpeg">
    <h2>{{club}}</h2>
    <p>Peoples inside: {{value}}</p>
</a>

我的问题是我没有得到,如何按其值对对象属性进行排序。我知道Angular中有默认的过滤器orderBy,但我不知道如何在我的情况下使用它

2 个答案:

答案 0 :(得分:3)

将数据更改为对象数组

[
  { name: 'name 1', count:12},
  { name: 'name 2', count:66},
  { name: 'name 3', count:66}
]

然后你可以在数组上使用orderBy

<a ng-repeat="club in onlineInClubsCount| orderBy:'count'">
  {{club.name}}

答案 1 :(得分:0)

如文档中所述,您可以将对象转换为使用此过滤器排序的数组: toArrayFilter

angular.module('app', ['angular-toArrayFilter'])

  <div ng:repeat="item in onlineInClubsCount | toArray | orderBy: '$value'" class="item item-avatar" ng-href="#/app/clubs/{{item.$key}}">
    <img ng-src="img/tc-logo.jpeg">
    <h2>{{item.$key}}</h2>
    <p>People inside: {{item.$value}}</p>
  </div>


$scope.onlineInClubsCount = {
    name: 12,
    anotherName:2,
    theOther:74
};