AngularJS ngOptions 排序数组 [英] AngularJS ngOptions sort array
本文介绍了AngularJS ngOptions 排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试使用 ng-options
在选择中显示一组字体,按数组中项目的值按字母顺序排序.
I have been trying to use ng-options
to display an array of fonts in a select sorted alphabetically by the value of the items in the array.
HTML
<select ng-options="font for font in webfonts | orderBy:'font'" name="fonts">
<option value="">Choose a font</option>
</select>
JS
$scope.webfonts = [
'Abel', 'Crafty Girls' , 'Lato' , 'Average',
'Corben', 'Quicksand', ... ];
我尝试过更改 orderBy
和其他东西中的值.我已阅读文档和所有评论.
I've tried changing the value in orderBy
and other things. I've read through the documentation and all comments.
我错过了什么?这应该只适用于对象吗?
What am I missing? Is this supposed to only work on objects?
推荐答案
这是你需要做的:
<select ng-model="selected" ng-options="font for font in webfonts | orderBy:'toString()' " name="fonts">
- 您需要添加 ng-model 才能正确地使绑定适用于字符串列表.
- 如果输入包含字符串列表,您可以使用
toString()
进行排序.由于orderBy
的表达式可以是一个 Getter 函数.此函数的结果将使用 <, =, > 运算符进行排序.
- You need to add ng-model to correctly make the binding works for a list of strings.
- You can use
toString()
to sort if the input contains a list of strings. Since the expression oforderBy
can be a Getter function. The result of this function will be sorted using the <, =, > operator.
这篇关于AngularJS ngOptions 排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文