AngularJS ngOptions sort array

后端 未结 2 2135
北荒
北荒 2020-12-15 04:58

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

相关标签:
2条回答
  • 2020-12-15 05:33

    This is what you need to do:

    <select ng-model="selected" ng-options="font for font in webfonts | orderBy:'toString()' " name="fonts">
    
    1. You need to add ng-model to correctly make the binding works for a list of strings.
    2. You can use toString() to sort if the input contains a list of strings. Since the expression of orderBy can be a Getter function. The result of this function will be sorted using the <, =, > operator.

    Demo

    0 讨论(0)
  • 2020-12-15 05:36

    As the documentation specifies, the string argument is for object properties, not for primitives. I think, as elementary as it sounds, you have to create a function on the scope that simply returns the argument, and pass that to orderBy.

    See jsFiddle!

    0 讨论(0)
提交回复
热议问题