【问题标题】:Angularjs orderBy with integer Strings带有整数字符串的Angularjs orderBy
【发布时间】:2015-04-17 13:18:25
【问题描述】:

我有一个 Ex 的数据集:

$scope.friends =
      [{name:'John',  score:'10'},
       {name:'Mary',  score:'19'},
       {name:'Mike',  score:'-21'},
       {name:'Adam',  score:'-35'},
       {name:'Julie', score:'29'}];
}]);

我将此数据用作ng-repeat的来源,

<tr ng-repeat="friend in friends | orderBy:'-score' ">
  <td>{{friend.name}}</td>
  <td>{{friend.score}}</td>
</tr>

我想按照descending 中的score 顺序对friends 进行排序。如下图,

Name    Score
-------------
Julie   29
Mary    19
John    10    
Mike    -21
Adam    -35

但我得到的输出为,

Name    Score
-------------
Julie   29
Mary    19
John    10  
Adam    -35  
Mike    -21

这是Demo Plunker

请注意 -21 和 -35 在输出中的顺序不正确,这是因为 score 属性是 String 值。如果我将其更改为 int 值,那么一切都按预期工作。如何克服这个问题,请考虑我无法更改 score 属性的类型。

【问题讨论】:

  • 这似乎是 Angular 中有效的 open issue。提到了一些解决它的建议。

标签: angularjs angularjs-ng-repeat angularjs-orderby


【解决方案1】:

怎么样

<tr ng-repeat="friend in friends | orderBy:'+-score' ">
  <td>{{friend.name}}</td>
  <td>{{friend.score}}</td>
</tr>

Plunker

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多