先看看代码吧:
//按时间排序
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="itemList in lists | orderBy :'time':true track by $index ">
<span>{{itemList.time}}</span>------
<span ng-repeat="item in itemList.data track by $index">{{$index==0?item+'%':item}}-</span>
</div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.lists = [{"time":"2016-04-11","data":["29.07","50439","50439","1170","22544","21245","912"]},{"time":"2016-04-12","data":["55.15","87668","74705","2222","38019","35530","1671"]},{"time":"2016-04-13","data":["31.73","50720","44320","1175","23042","21743","893"]}]
});
</script>
</body>
</html>
结果:
2016-04-13------ 31.73%-50720-44320-1175-23042-21743-893-
2016-04-12------ 55.15%-87668-74705-2222-38019-35530-1671-
2016-04-11------ 29.07%-50439-50439-1170-22544-21245-912-
本来正常排序按照angularJS的文档讲解那样加个过滤器就行,但这里为什么加了track by $index
,这是因为angularjs里不允许遍历里的数组里有相同的结果。如果不加,则结果里的第三条数据因为50439是相同的,则不会显示了。不加效果如下:
2016-04-13------ 31.73%-50720-44320-1175-23042-21743-893-
2016-04-12------ 29%-87668-74705-2222-38019-35530-1671-
2016-04-11------