如何按键对ng-options中的数组进行排序?

How to sort array in ng-options by key?

有这样的数组:

month: Array[13]0: "M"1: "January"2: "February"3: "March"4: "April"5: "May"6: "June"7: "July"8: "August"9: "September"10: "October"11: "November"12: "December"

我愿意:

ng-options="key as value for (key, value) in data.month | orderBy:key"

但我得到未排序的 select 列表。

要使用带过滤器的跟踪,请使用 track by

标记

ng-options="key as value for (key, value) in data.month | orderBy:'key' track by key"

更新

这个 orderBy 永远不会工作,因为你有文字数组。您需要将此数组转换为 JSON 之类的对象,其结构类似于 [{id: 0, value: "M"}, {id: 1, value: "January"},......]

HTML

ng-options="item.id as item.value for items in data.month | orderBy:'id'"

Demo Plunkr

你少了两个 '。这部分,orderBy: key应该是orderBy:'key'

试试这个:

ng-options="key as value for (key, value) in data.month | orderBy: 'key'"

要解决此问题,需要重新格式化 ng-option,例如:

day.id as day.value for day in data.month

而数组 data.month 将是如何告诉用户 pankajparkar