AngularJS orderBy does not work with track by in ngOptions?

Angularjs

Angularjs Problem Overview


I am trying to sort ngOptions with track by

This is my template

<select ng-model="asd" ng-options="user.id as user.name for user in users track by user.id | orderBy: 'name'">

This is my controller

function AppCtrl($scope) {
  $scope.users = [
   {id : 25, name: 'Batista'},
   {id : 26, name: 'Ultimate Warrior'},
   {id : 27, name: 'Andre the giant'}
  ];
  $scope.name = 'asdasd';
  $scope.asd = 25;
 }

I wrote a snippet in JSBin to demonstrate this. The problem with this is the sorting does not work. Should I write a custom filter?

Angularjs Solutions


Solution 1 - Angularjs

In order to use tracking with filters, the track by expression needs to be added after the filter.

Try this instead:

user.id as user.name for user in users | orderBy: 'name' track by user.id

The documentation for ngRepeat mentions this under the "Arguments" section, specifically:

> Filters should be applied to the expression, before specifying a tracking expression.

and

> For example: item in items | filter:searchText track by item.id is a pattern that might be used to apply a filter to items in conjunction with a tracking expression.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontastebudsView Question on Stackoverflow
Solution 1 - AngularjsAhmad MageedView Answer on Stackoverflow