gpt4 book ai didi

AngularJS orderBy 不能与 ngOptions 中的 track by 一起使用?

转载 作者:行者123 更新时间:2023-12-03 05:23:20 24 4
gpt4 key购买 nike

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

这是我的模板

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

这是我的 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;
}

我写了一个snippet in JSBin来证明这一点。这样做的问题是排序不起作用。我应该编写自定义过滤器吗?

最佳答案

为了使用过滤器进行跟踪,需要在过滤器后面添加 track by 表达式。

试试这个:

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

ngRepeat 的文档在“参数”部分提到了这一点,特别是:

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

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.

关于AngularJS orderBy 不能与 ngOptions 中的 track by 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23925445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com