11

There is such array:

month: Array[13]0: "M"1: "January"2: "February"3: "March"4: "April"5: "May"6: "June"7: "July"8: "August"9: "September"10: "October"11: "November"12: "December"

I do:

ng-options="key as value for (key, value) in data.month | orderBy:key"

But I get unsorted select list.

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
Faud
  • 451
  • 3
  • 7
  • 16

3 Answers3

17

In order to use tracking with filters use track by.

Markup

ng-options="key as value for (key, value) in data.month | orderBy:'key' track by key"

Update

This orderBy will never work because you are having literal array. You need to convert this array to JSON like object which would structured like [{id: 0, value: "M"}, {id: 1, value: "January"},......]

HTML

ng-options="item.id as item.value for items in data.month | orderBy:'id'"

Demo Plunkr

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • I get empty select list by your solution – Faud Jun 03 '15 at 13:15
  • @Faud try adding single `'` on key as @solid_luffy said – Pankaj Parkar Jun 03 '15 at 13:16
  • Also is same. Not working. I get error: Error: $parse:lexerr Lexer Error – Faud Jun 03 '15 at 13:42
  • I have format : `"day":[{"id":1,"value":1},{"id":2,"value":2}` but get empty list with error: `Error: [$parse:lexerr] http://errors.angularjs.org/1.3.13/$parse/lexerr?p0=Unterminated%20quote&p1=s%203-4%20%5B'%5D&p2=key'` – Faud Jun 03 '15 at 14:05
  • @Faud could you accept mine answer..I'll give you working fiddle/plunkr – Pankaj Parkar Jun 03 '15 at 15:06
  • @Faud check the plunkr which I've added..Thanks – Pankaj Parkar Jun 03 '15 at 15:45
  • The plunkr is an `ng-repeat`; similar but not what this question asks. Also did you mean `item.id as item.value for items` in your html template snippet or `item.id as item.value for item` (no `s` on final `item`)? – ruffin Jun 30 '22 at 13:59
2

Your missing two ''s. This part, orderBy: key should be orderBy:'key'

Try this:

ng-options="key as value for (key, value) in data.month | orderBy: 'key'"
solid_luffy
  • 361
  • 2
  • 15
1

To resolve this issue need reformat ng-option like as:

day.id as day.value for day in data.month

And array data.month would be how as told user pankajparkar

Faud
  • 451
  • 3
  • 7
  • 16