Angularjs orderBy 与整数字符串

标签 angularjs angularjs-ng-repeat angularjs-orderby

我有一个 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>

我想根据分数降序顺序对 friend 进行排序。就像下面这样,

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值(value)。如果我将其更改为 int 值,那么所有内容都会按预期工作。如何克服这个问题,请考虑我无法更改 score 属性的类型。

最佳答案

怎么样

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

Plunker

关于Angularjs orderBy 与整数字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29700353/

相关文章:

node.js - Angularjs REST API交互失败

javascript - ng-repeat 指令中的 Angular 选择 : how can I access the children scopes?

javascript - ng-repeat 中按键自定义对象排序

javascript - Angularjs 在使用分页时无法显示 orderBy

javascript - Angular.js ng-repeat 验证条件中的多个输入

javascript - AngularJS:OrderBy Descending(空条目)

javascript - ng-options 和 ng-model 预选值

javascript - AngularJS 从 PHP 文件获取数据

angularjs - ng-click 后如何切换字段必需属性?

css - ng-repeat 中 li 项的缩进