我有这个数据:
[{"id":"42","firstname":"Sarah","lastname":"Dilby","age":"40","cars":"Yaris"},
{"firstname":"Jason","lastname":"Diry","age":"5","id":"5"},
{"id":"6","firstname":"Bilson","lastname":"Berby","age":"1","cars":"Tipo"}]
当我在 ng-repeat 中按 id 或按年龄排序时,它会将数字排序为文本。由于我在任何地方都找不到书面说明这是一个问题,我猜我的代码有问题。我已经创建了这个小提琴: http: //jsfiddle.net/vsbGH/1/对不起模板,但 jsfiddle 不允许在 html 框中。无论如何,这是加载和排序数据的代码:
//user data
app.service('People', function() {
var People = {};
People.details = [{"id":"42","firstname":"Sarah","lastname":"Dilby","age":"40","cars":"Yaris"},
{"firstname":"Jason","lastname":"Diry","age":"5","id":"5"},
{"id":"6","firstname":"Bilson","lastname":"Berby","age":"1","cars":"Tipo"}]
return People;
});
//list ctrl
controllers.listCtrl = function ($scope,People) {
$scope.people = People.details;
$scope.sortList = function(sortname) {
$scope.sorter = sortname;
}
}
这是模板的 ng-repeat 部分:
<tr ng-repeat="person in people | orderBy:sorter ">
<td>{{person.id | number}}</td>
<td>{{person.firstname}} </td>
<td>{{person.lastname}} </td>
<td>{{person.age | number}}</td>
<td>{{person.cars}} </td>
</tr>
非常感谢您能帮助我理解为什么数字数据没有按数字排序,以及为什么它按文本排序。