AngularJS ngOptions sort array
This is what you need to do:
<select ng-model="selected" ng-options="font for font in webfonts | orderBy:'toString()' " name="fonts">
- 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.
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!