【问题标题】:ng-repeat and orderBy a Stringng-repeat 和 orderBy 一个字符串
【发布时间】:2016-03-08 03:41:40
【问题描述】:

我从我的 API 中返回了两个项目,一个有一个 startDate,一个有一个 endDate,我想知道是否可以通过参数字符串对它们进行排序?

数据:

[
  {
    name: "Item 1",
    type: "Start Time"
  },
  {
    name: "Item 2",
    type: "End Time"
  }
]

类似这样的:

<li ng-repeat="item in items | orderBy: type='Start Time'">{{ item.name }}</li>

是否可以使用 Angular 的 orderBy 过滤器?

感谢任何帮助。

【问题讨论】:

标签: angularjs string angularjs-orderby


【解决方案1】:

您只需在orderBy 中指定要订购的密钥:

orderBy: 'type'

如果你想颠倒顺序使用-:

orderBy: '-type'

【讨论】:

  • 即使数据被翻转也能正常工作。而Item 1 的类型为End TimeItem 2 的类型为Start Time
  • 是的,orderBy 仅取决于用于订购的密钥
【解决方案2】:

不需要指定开始时间,

<li ng-repeat="item in items | orderBy: 'type'>{{ item.name }}</li>

这是工作的Application

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 2015-02-11
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    相关资源
    最近更新 更多