我使用可與ngRepeat結合使用的orderBy指令對一些數字字符串進行反向排序。使用AngularJS'orderBy'排序減號字符串
<div ng-repeat="item in standings| orderBy:['-points', '-gd', 'team_name']">
<p>{{item.gd}}
</div>
所以它是排序(按優先順序排列) '點',然後 '的gd'(淨勝球),然後選擇 'TEAM_NAME'(按字母順序)。
gd
值按降序正確排序。我遇到的問題是負數值。在這種情況下,數字作爲字符串返回,並且orderBy函數不理解"-2"
小於"-1"
,但相反。
如何獲得ng-repeat將數值解析爲整數,特別是用負數解決這個順序問題?
我考慮製作一個過濾器,例如:{{item.gd | *filterHere* }}
,但這不會被初始ng-repeat指令看到,它需要將源值作爲整數。
任何幫助,將不勝感激。
UPDATE:
我試過這個過濾器,但是當我把它在我的NG-重複,它沒有返回值:
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()輕鬆完成此操作:http://www.w3schools.com/jsref/jsref_parseint.asp – Mistalis
謝謝。我試着用這個自定義過濾器,但它沒有工作 - 請看我的更新... – Paulos3000