【发布时间】:2016-06-14 15:09:23
【问题描述】:
我正在使用可与 ngRepeat 结合使用的 orderBy 指令对一些数字字符串进行反向排序。
<div ng-repeat="item in standings| orderBy:['-points', '-gd', 'team_name']">
<p>{{item.gd}}
</div>
所以它是排序(按优先级顺序)“points”,然后是“gd”(目标差异),然后是“team_name”(按字母顺序)。
gd 值正确地按降序排列。我遇到的问题是负数值。在这种情况下,数字作为字符串返回,orderBy 函数不会将 "-2" 理解为小于 "-1",而是相反。
如何让 ng-repeat 将数值解析为整数,尤其是解决这个负数的排序问题?
我考虑过做一个过滤器,例如:{{item.gd | *filterHere* }},但是初始的 ng-repeat 指令看不到这个,它需要将源值作为整数。
任何帮助将不胜感激。
更新:
我试过这个过滤器,但是当我在我的 ng-repeat 中调用它时,它什么也没返回:
app.filter('stringToInteger', function() {
return function(input) {
angular.forEach(input, function(value) {
parseInt(value.gd);
})
};
return input;
});
在视图中:
<p ng-repeat="item in standings | orderBy: '-gd' | stringToInteger">GD = {{item.gd}}</p>
【问题讨论】:
-
为什么不直接在 JavaScript 中将
standing.gd设置为int?您可以使用 parseInt() 轻松完成此操作:w3schools.com/jsref/jsref_parseint.asp -
谢谢。我尝试将它与自定义过滤器一起使用,但它不起作用 - 请查看我的更新...
标签: angularjs string integer ng-repeat