AngularJS orderBy 在 ngOptions 中不能使用 track by?

我正在尝试用 track byngOptions进行排序

这是我的模板

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

这是我的控制器

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;
}

我写了一个 JSBin 中的代码片段来演示这一点。这样做的问题是排序不起作用。我应该编写一个自定义过滤器吗?

26628 次浏览

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.