在AngularJs中按date降序sorting

<div class="recent" ng-repeat="reader in (filteredItems = (book.reader | orderBy: 'created_at' | limitTo: 1))"> </div> 

所以这本书来自于restapi,它附有许多读者。 我想得到'最近'的读者。

created_at字段具有将用户识别为最近的值。 但是上面的代码给了我最老的读者。 所以顺序需要被颠倒? 有没有办法按降序sorting?

根据文档,你可以使用reverse参数。

 filter:orderBy(array, expression[, reverse]); 

将您的filter更改为:

 orderBy: 'created_at':true 

你可以在orderBy加上一个“ – ”作为参数的前缀,而不是升序。 我会写这样的:

 <div class="recent" ng-repeat="reader in book.reader | orderBy: '-created_at' | limitTo: 1"> </div> 

这也在filterorderBy的文档中说明。

也许这可能对某人有用:

在我的情况下,我得到一个对象数组,每个对象包含一个由Mongoose设置的date。

我用了:

 ng-repeat="comment in post.comments | orderBy : sortComment : true" 

并定义了function:

 $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/

降序按datesorting

这将有助于按降序过滤datelogging。

 $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是由一个select框决定的。 我更喜欢路德维希的回答,因为你可以在select选项中设置sorting方向:

  $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

然后添加“反向”标志:

 <!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>