我一直在尝试使用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">
  • 您需要添加ng-model才能正确使绑定(bind)适用于字符串列表。
  • 如果输入包含字符串列表,则可以使用toString()进行排序。由于orderBy的表达式可以是Getter函数。该函数的结果将使用运算符进行排序。

  • Demo

    关于angularjs - AngularJS ngOptions排序数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18260920/

    10-09 15:24
    查看更多