Answers:
根据文档,您可以使用reverse
参数。
filter:orderBy(array, expression[, reverse]);
将您的过滤器更改为:
orderBy: 'created_at':true
您可以在参数前orderBy
加一个'-'使其具有降序而不是升序。我会这样写:
<div class="recent"
ng-repeat="reader in book.reader | orderBy: '-created_at' | limitTo: 1">
</div>
过滤器orderBy的文档中也对此进行了说明。
也许这对某人可能有用:
就我而言,我得到了一个对象数组,每个对象包含由猫鼬设置的日期。
我用了:
ng-repeat="comment in post.comments | orderBy : sortComment : true"
并定义了功能:
$scope.sortComment = function(comment) {
var date = new Date(comment.created);
return date;
};
这对我有用。
和一个代码示例:
<div ng-app>
<div ng-controller="FooController">
<ul ng-repeat="item in items | orderBy:'num':true">
<li>{{item.num}} :: {{item.desc}}</li>
</ul>
</div>
</div>
和JavaScript:
function FooController($scope) {
$scope.items = [
{desc: 'a', num: 1},
{desc: 'b', num: 2},
{desc: 'c', num: 3},
];
}
会给你:
3 :: c
2 :: b
1 :: a
在JSFiddle上:http : //jsfiddle.net/agjqN/
按日期降序
这将有助于按日期降序过滤记录。
$scope.logData = [
{ event: 'Payment', created_at: '04/05/17 6:47 PM PST' },
{ event: 'Payment', created_at: '04/06/17 12:47 AM PST' },
{ event: 'Payment', created_at: '04/05/17 1:50 PM PST' }
];
<div ng-repeat="logs in logData | orderBy: '-created_at'" >
{{logs.event}}
</div>
在我的情况下,orderBy由选择框确定。我更喜欢Ludwig的回答,因为您可以在select选项中设置排序方向,如下所示:
$scope.options = [
{ label: 'Title', value: 'title' },
{ label: 'Newest', value: '-publish_date' },
{ label: 'Featured', value: '-featured' }
];
标记:
<select ng-model="orderProp" ng-options="opt as opt.label for opt in options"></select>
<ul>
<li ng-repeat="item in items | orderBy:orderProp.value"></li>
</ul>
请参阅w3schools示例:https : //www.w3schools.com/angular/angular_filters.asp https://www.w3schools.com/angular/tryit.asp?filename=try_ng_filters_orderby_click
然后添加“ reverse”标志:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<p>Click the table headers to change the sorting order:</p>
<div ng-app="myApp" ng-controller="namesCtrl">
<table border="1" width="100%">
<tr>
<th ng-click="orderByMe('name')">Name</th>
<th ng-click="orderByMe('country')">Country</th>
</tr>
<tr ng-repeat="x in names | orderBy:myOrderBy:reverse">
<td>{{x.name}}</td>
<td>{{x.country}}</td>
</tr>
</table>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
$scope.reverse=false;
$scope.orderByMe = function(x) {
if($scope.myOrderBy == x) {
$scope.reverse=!$scope.reverse;
}
$scope.myOrderBy = x;
}
});
</script>
</body>
</html>
var myApp = angular.module('myApp', []);
myApp.filter("toArray", function () {
return function (obj) {
var result = [];
angular.forEach(obj, function (val, key) {
result.push(val);
});
return result;
};
});
myApp.controller("mainCtrl", function ($scope) {
$scope.logData = [
{ event: 'Payment', created_at: '10/10/2019 6:47 PM PST' },
{ event: 'Payment', created_at: '20/10/2019 12:47 AM PST' },
{ event: 'Payment', created_at: '30/10/2019 1:50 PM PST' }
];
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="mainCtrl">
<h4>Descending</h4>
<ul>
<li ng-repeat="logs in logData | toArray | orderBy:'created_at':true" >
{{logs.event}} - Date : {{logs.created_at}}
</li>
</ul>
<br>
<h4>Ascending</h4>
<ul>
<li ng-repeat="logs in logData | toArray | orderBy:'created_at':false" >
{{logs.event}} - Date : {{logs.created_at}}
</li>
</ul>
</div>
:
不是逗号。(对于其他非观察员)