如何按键对 ng-options 中的数组进行排序? [英] How to sort array in ng-options by key?
本文介绍了如何按键对 ng-options 中的数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有这样的数组:
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"
但是我得到了未排序的选择列表.
But I get unsorted select list.
推荐答案
为了使用带有过滤器的跟踪,请使用 track by
.
In order to use tracking with filters use 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"},......]代码>
This orderBy
will never work because you are having literal array. You need to convert this array to JSON like object which would structured like [{id: 0, value: "M"}, {id: 1, value: "January"},......]
HTML
ng-options="item.id as item.value for items in data.month | orderBy:'id'"
这篇关于如何按键对 ng-options 中的数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文