I’m populating a dropdown through the use of ng-options which is hooked to a controller that in turn is calling a service. Unfortunately the data coming in is a mess and I need to be able to sort it alphabetically.
You figure that something like $.sortBy would do it but unfortunately it didn’t do jack. I know I can sort it via javascript with a helper method function asc(a,b) or something like that but I refuse to believe that there is not cleaner way of doing this plus I don’t want to bloat the controller with helper methods. It is something so basic in principle so I don’t understand why AngularJS doesn’t have this.
Is there a way of doing something like $orderBy('asc')?
Example:
<select ng-option="items in item.$orderBy('asc')"></select>
It would be extremely useful to have options in orderBy so you can do whatever you want, whenever you usually try to sort data.
Angular has an orderBy filter that can be used like this:
See this fiddle for an example.
It’s worth noting that if
track byis being used it needs to appear after theorderByfilter, like this: