AngularJS ORDERBY不与轨道在ngOptions工作? [英] AngularJS orderBy does not work with track by in ngOptions?

查看:153
本文介绍了AngularJS ORDERBY不与轨道在ngOptions工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过

I am trying to sort ngOptions with track by

这是我的模板

<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片段来证明这一点。与此问题是分拣不起作用。我应该写一个自定义过滤器?

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?

推荐答案

在为了使用过滤器跟踪,需要添加由前pression的轨道后过滤器。

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

试试这个:

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

有关 ngRepeat 提到了这个文档在参数部分中,特别是:

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

过滤器应适用于前pression,指定跟踪前pression之前。

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

例如:中的项项|过滤:通过item.id 的SEARCHTEXT轨道是一个可能被用来在跟踪前pression一起应用过滤器项目的模式

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工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆