json AngularJS ng-repeat orderBy 日期不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24048590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:20:53  来源:igfitidea点击:

AngularJS ng-repeat orderBy date not working

jsonangularjs

提问by eat-sleep-code

I have a working ng-repeat, that I would like to order by descending date (newest items first).

我有一个工作 ng-repeat,我想按降序排序(最新的项目在前)。

However, I can not seem to get it to work. I have double checked quoting, etc. I have tried orderBy:'createdate':reverse, orderBy:'article.createdate':reverse, and orderBy:'data.blog.article.createdate':reverseNone seem to work.

但是,我似乎无法让它发挥作用。我仔细检查了引用等。我试过orderBy:'createdate':reverse, orderBy:'article.createdate':reverse, 和orderBy:'data.blog.article.createdate':reverseNone 似乎工作。

This is my view:

这是我的观点:

<article data-ng-if="article.id == post || post===NULL" data-ng-repeat="article in data.blog.article | orderBy:'createdate':reverse | slice:currentPage*pageSize:currentPage+1*pageSize">
    <h2 class="blog-post-title">
        <a href="#!/blog/{{article.id}}" title="Permalink to {{article.title.__cdata}}">{{article.title.__cdata}}</a>
    </h2>
    <span class="blog-post-meta">Posted on {{article.createdate | date:'MMMM dd, yyyy h:mma'}} by {{article.author}}</span>
    <div class="blog-post-content" ng-bind-html="article.content.__cdata">{{article.content.__cdata}}</div> 
</article>

Here is a sample of the data (converted to JSON from XML using X2JS):

以下是数据示例(使用 X2JS 从 XML 转换为 JSON):

{
    "blog": {
        "article": [
            {
                "id": "1",
                "author": "eat-sleep-code",
                "title": {
                    "__cdata": "The first article."
                },
                "content": {
                    "__cdata": "\n        This is my first article in my test site.\n      "
                },
                "createdate": "2014-05-09"
            },
            {
                "id": "2",
                "author": "eat-sleep-code",
                "title": {
                    "__cdata": "The second article."
                },
                "content": {
                    "__cdata": "\n        This is my second article in my test site.  This article's create date is actually earlier.\n      "
                },
                "createdate": "2014-05-08"
            }
        ]
    }
}

回答by Marc Kline

The reverseargument should be a boolean value.

reverse参数应该是一个布尔值。

Assuming you don't have reverse set to true or false somewhere in your controller, you should replace

假设您的控制器中的某处没有将 reverse 设置为 true 或 false,您应该替换

orderBy:'createdate':reverse

with

orderBy:'createdate':true

Demo

演示

orderBy docs

按文档排序