AngularJS ng-repeat orderBy date not working

时间:2014-06-04 22:21:57

标签: json angularjs

我有一个有效的重复,我想按降序排序(最新的项目)。

然而,我似乎无法让它发挥作用。我有双重检查引用,等等。我尝试了orderBy:'createdate':reverseorderBy:'article.createdate':reverseorderBy:'data.blog.article.createdate':reverse似乎无效。

这是我的观点:

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

以下是数据示例(使用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"
            }
        ]
    }
}

1 个答案:

答案 0 :(得分:62)

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

假设您的控制器中没有反向设置为true或false,则应替换

orderBy:'createdate':reverse

orderBy:'createdate':true

Demo

orderBy docs