Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ngOptions sort array

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?

like image 376
Chris Bier Avatar asked Aug 15 '13 20:08

Chris Bier


People also ask

How do I sort an array in AngularJS?

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.

How to use sorting in AngularJS?

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 .

How to use ng-options AngularJS?

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.

What does ng-options do?

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.

What is the use of ng options in angular?

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 ...

How do you sort an array in angular?

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.

How to bind dropdown list using ng-options directive in AngularJS?

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.

What is the use of ngoptions attribute?

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:


2 Answers

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

like image 106
zs2020 Avatar answered Nov 16 '22 00:11

zs2020


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!

like image 32
Steve Klösters Avatar answered Nov 15 '22 22:11

Steve Klösters