我一直在尝试使用ng-options
在选择中显示字体数组,该字体数组按数组中各项的值按字母顺序排序。
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
和其他内容的值。我已经阅读了documentation和所有评论。我想念什么?这应该仅适用于对象吗?
最佳答案
这是您需要做的:
<select ng-model="selected" ng-options="font for font in webfonts | orderBy:'toString()' " name="fonts">
toString()
进行排序。由于orderBy
的表达式可以是Getter函数。该函数的结果将使用运算符进行排序。 Demo
关于angularjs - AngularJS ngOptions排序数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18260920/