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?
String: If the array is an array of objects, you can sort the array by the value of one of the object properties. See the examples below. Function: You can create a function to organize the sorting. Array: Use an array if you need more than one object property to determine the sorting order.
To sort in descending order, set it as true . You can also use + to sort the data in an ascending and – the data in descending order also . Here with the filters in Angular JS, instead of displaying the various rows, we will be sorting it by ascending and descending order .
AngularJS ng-options DirectiveThe ng-options directive fills a <select> element with <options>. The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when using the ng-options directive.
The ng-options Directive in AngularJS is used to build and bind HTML elements with options to a model property. It is used to specify <options> in a <select> list. It is designed specifically to populate the items on a dropdown list. This directive implements an array, in order to fill the dropdown list.
AngularJS ng-options Directive 1 Definition and Usage. The ng-options directive fills a <select> element with <options>. The ng-options directive uses an... 2 Syntax. Supported by the <select> element. 3 Parameter Values. An expression that selects the specified parts of an array to fill the select element. More ...
Array Sort method sort the arrays in ascending or descending order. Here is html component. In Angular, any object can be compared using the relational operator ( === ) In the same way, we can use the === operator to compare objects.
While using ng-options in angularjs optionally we need to define one hard coded <option> element with value as empty string in <select> element. This hard coded empty string value will represent starting value of dropdown list. Following is the syntax of using ng-options directive in angularjs to bind dropdown list.
The ngOptions attribute is used to dynamically generate a list of <option> elements for the <select> element using the array or object obtained by evaluating the ngOptions comprehension expression. With ng-options the markup can be reduced to just a select tag and the directive will create the same select:
This is what you need to do:
<select ng-model="selected" ng-options="font for font in webfonts | orderBy:'toString()' " name="fonts">
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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With