使用object属性过滤ng-repeat

时间:2016-11-07 19:24:31

标签: angularjs angularjs-ng-repeat

我有一个如下所示的数组:

$scope.athletes= [
{
   name: 'name 01',
   measurements: [
   {
      type: 'date'
   }, {
      type: 'info',
      value: 23,
      rank: 1
   }]
},
{
   name: 'name 02',
   measurements: [
   {
      type: 'date'
   }, {
      type: 'info',
      value: 13,
      rank: 3
   }]
},
 {
   name: 'name 03',
   measurements: [
   {
      type: 'date'
   }, {
      type: 'info',
      value: 17,
      rank: 2
   }]
}];

我想基于rank过滤器显示结果。我已经尝试了一些东西,但这不起作用。我的代码如下:

<tr ng-repeat="athlete in  filteredEntries = (athletes | filter: measurements[1].rank )">

   <td> {{ athlete.name }} </td>

    <td > {{ athlete.measurements[1].value }} </td>

</tr>

显示输出:

    Name      ---   value
 ===========================
    name 01   ---    23
 ---------------------------
    name 02   ---    13
 ---------------------------
    name 03   ---    17

但我想要以下输出:

    Name      ---   value
 ===========================
    name 01   ---    23
 ---------------------------
    name 03   ---    17
 ---------------------------
    name 02   ---    13

1 个答案:

答案 0 :(得分:0)

您应该使用orderBy,请查看以下jsfiddle:

https://jsfiddle.net/twizzlers/7peLbu2m/1/

<div ng-repeat="athlete in athletes | orderBy: 'measurements[1].rank' ">
   <div> {{ athlete.name }} </div>
   <div > {{ athlete.measurements[1].value }} </div>
</div>

希望有帮助=)