Angular 1.x: OrderBy 没有对日期进行排序
Angular 1.x: OrderBy isn't ordering the dates
我们如何使用 '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/2018
2019-01-22
02/02/2018
06/06/2018
12/12/2018
异常输出
01/02/2018
02/02/2018
06/06/2018
12/12/2018
2019 年 1 月 22 日
您需要将字符串转换为日期并进行排序
<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>
我们如何使用 '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/2018 2019-01-22 02/02/2018 06/06/2018 12/12/2018
异常输出 01/02/2018 02/02/2018 06/06/2018 12/12/2018 2019 年 1 月 22 日
您需要将字符串转换为日期并进行排序
<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>