Angular 1.x: OrderBy не упорядочивает даты - PullRequest
0 голосов
/ 24 января 2019

Как мы можем использовать метод 'filter', чтобы упорядочить даты в возрастающем или убывающем порядке?

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> 
</script>
<body>
<div ng-app="myApp" ng-controller="orderCtrl">
<ul><li ng-repeat="x in dates | orderBy">{{x}}</li></ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
$scope.dates = ["01/02/2018", "02/02/2018", "06/06/2018", "01/22/2019",     "12/12/2018"];
});
</script>
</body>
</html>

Получение выходных данных от 01.02.2008 г. 01.02.2017 02/02 /2018 06/06/2018 12/12/2018

Исключенный выходной 01/02/2018 02/02/2018 06/06/2018 12/12/2018 01/22/2019

1 Ответ

0 голосов
/ 24 января 2019

Вам нужно преобразовать строку в дату и выполнить сортировку

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> 
</script>
<body>
<div ng-app="myApp" ng-controller="orderCtrl">
<ul><li ng-repeat="x in dates | orderBy : sort : false">{{x}}</li></ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
$scope.dates = ["01/02/2018", "02/02/2018", "06/06/2018", "01/22/2019","12/12/2018"];
$scope.sort = function(date) {
    var date = new Date(date);
    return date;
};
});
</script>
</body>
</html>
...