AngularJS ngOptions sort array

angularjs, ng-options

Solution

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 of `orderBy` can be a Getter function. The result of this function will be sorted using the <, =, > operator.

Demo

Problem

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', ... ]; ``` 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?

Original source