在AngularJS按字母顺序排序下拉列表 [英] Sorting dropdown alphabetically in AngularJS

查看:962
本文介绍了在AngularJS按字母顺序排序下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林填充通过使用纳克选项是钩住在又调用服务的控制器的下拉菜单。不幸的是即将到来的数据是一个烂摊子,我需要能够按字母顺序排序。

I'm populating a dropdown through the use of ng-options which is hooked to a controller that in turn is calling a service. Unfortunately the data coming in is a mess and I need to be able to sort it alphabetically.

您的身影,像 $。sortBy 会做但遗憾的是它没有这样做插孔。我知道我可以通过一个辅助方法,函数ASC的JavaScript(A,B)或类似的排序,但我不相信有这样做的不干净的方式再加上我不想用膨胀helper方法控制器。这东西在原则上如此基本,所以我不明白为什么AngularJS没有这样做。

You figure that something like $.sortBy would do it but unfortunately it didn't do jack. I know I can sort it via javascript with a helper method function asc(a,b) or something like that but I refuse to believe that there is not cleaner way of doing this plus I don't want to bloat the controller with helper methods. It is something so basic in principle so I don't understand why AngularJS doesn't have this.

有没有做这样的事情 $排序依据('ASC')

例如:

<select ng-option="items in item.$orderBy('asc')"></select>

这将是在排序依据选项,这样你可以做任何你想要的,只要你平时尽量对数据进行排序是非常有用的。

It would be extremely useful to have options in orderBy so you can do whatever you want, whenever you usually try to sort data.

推荐答案

角有一个排序依据过滤器可以使用的这样的:

Angular has an orderBy filter that can be used like this:

<select ng-model="selected" ng-options="f.name for f in friends | orderBy:'name'"></select>

请参阅这个提琴一个例子。

值得注意的是,如果使用跟踪它需要的排序依据过滤器后出现,像这样

It's worth noting that if track by is being used it needs to appear after the orderBy filter, like this:

<select ng-model="selected" ng-options="f.name for f in friends | orderBy:'name' track by f.id"></select>

这篇关于在AngularJS按字母顺序排序下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆