格式化对象键到月份,按时间顺序显示每个月份

AngularJS - Format Object Key to Month to Display each Month Chronologically

本文关键字:顺序 显示 时间 对象 格式化      更新时间:2023-09-26

我试图格式化一个对象键到月,并按月按时间顺序显示它。不太确定我做错了什么。下面是视图中的代码:

<div ng-repeat="month in months | orderBy: 'english' | date: 'MMMM' ">
     <h1><strong>{{ month.english }}</strong></h1>
     <p><strong>French Word: </strong>{{ month.french }}</p>
     <p><strong>Number of Days: </strong>{{ month.days }}</p>
     <hr />
</div>

下面是对象的示例:

angular
    .module('myApp', [])
    .controller('myCtrl', function($scope) {
        var months = [
            {
                english: 'August',
                french: 'Aout',
                days: 31,
                ordinal: 8,
                season: 'summer'
            },
            {
                english: 'March',
                french: 'Mars',
                days: 31,
                ordinal: 3,
                season: 'spring'
            },
            {
                english: 'February',
                french: 'Fevrier',
                days: 28,
                ordinal: 2,
                season: 'winter'
            }
        ];
        $scope.months = months;
    });

到目前为止,它是按字母而不是月份排序的。我哪里做错了?

您对日期筛选器的使用不正确。日期过滤器接受的唯一输入是date对象、时间戳或ISO 8601日期时间字符串。

参见Angular文档。

要按时间顺序排序,既然已经有了数据,为什么不按序数排序呢?

<div ng-repeat="month in months | orderBy: 'ordinal'">

我想你应该按月号排序,而不是按名字。使用orderBy: 'ordinal'代替orderBy: 'english'