【问题标题】:AngularJS ng-repeat orderBy date not workingAngularJS ng-repeat orderBy date 不起作用
【发布时间】:2014-06-04 22:21:57
【问题描述】:

我有一个正常工作的 ng-repeat,我想按日期降序排序(最新项目优先)。

但是,我似乎无法让它工作。我已经仔细检查了引用等。我尝试过 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"
            }
        ]
    }
}

【问题讨论】:

  • 当你说不工作时,发生了什么?是否有任何控制台错误?它们的顺序不正确吗?
  • 完全没有控制台错误。只是顺序不对。哦,还尝试从 orderBy 中删除 :reversed 以确保这不是问题。没有变化。

标签: json angularjs


【解决方案1】:

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

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

orderBy:'createdate':reverse

orderBy:'createdate':true

Demo

orderBy docs

【讨论】:

  • 在这种情况下,AngularJS 文档被很好地混淆了。没有简单的静态反向示例,正如这里所问的那样。在他们提供的示例中,演示变量的名称与关键字('reverse')相同,因此人们混淆了两者。太棒了!
  • 你现在甚至可以使用 orderBy:'-createdate'。
猜你喜欢
  • 2017-01-12
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
  • 2014-03-10
  • 1970-01-01
相关资源
最近更新 更多