ng-repeat with track by 和 filter 以及 orderBy 不起作用 [英] ng-repeat with track by and filter and orderBy not working

查看:20
本文介绍了ng-repeat with track by 和 filter 以及 orderBy 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码.

http://jsfiddle.net/0tgL7u6e/

JavaScript

var myApp = angular.module('myApp',[]);函数 MyCtrl($scope) {$scope.nameFilter = '';$scope.contacts = [{名称:'GHI'},{名称:'DEF'},{名称:'ABC'},{名称:'JKL'}];}

查看

<div><input type="text" ng-model="nameFilter" placeholder="Search..."/></div><p ng-repeat="联系人跟踪中的联系人按 $index | filter: nameFilter | orderBy: name">{{ contact.name }}</p>

我不知道为什么订单不起作用以及为什么过滤器不起作用.

在另一个问题上,我读到了一些无法过滤或排序对象的内容.但是我有一个上面的对象数组.此外,它应该可以工作!?

有什么问题吗?

解决方案

要使用带过滤器的跟踪,必须在过滤器后添加跟踪表达式.

<p ng-repeat="contact in contacts | orderBy: 'name' | filter: nameFilter track by $index">{{ contact.name }}</p>

这是有效的fiddle

I have this code.

http://jsfiddle.net/0tgL7u6e/

JavaScript

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.nameFilter = '';
    $scope.contacts = [
        {name: 'GHI'},
        {name: 'DEF'},
        {name: 'ABC'},
        {name: 'JKL'}
    ];
}

View

<div ng-controller="MyCtrl">
    <div><input type="text" ng-model="nameFilter" placeholder="Search..." /></div>
    <p ng-repeat="contact in contacts track by $index | filter: nameFilter | orderBy: name">{{ contact.name }}</p>
</div>

I don't know why the order is not working and why the filter is not working.

At another question, I've read about something that objects can't be filtered or ordered. But I have an array of the objects above. Also, it should work!?

What's the problem?

解决方案

To use tracking with filters, the track by expression has to be added after the filter.

<p ng-repeat="contact in contacts | orderBy: 'name' | filter: nameFilter  track by $index">{{ contact.name }}</p>

Here is the working fiddle

这篇关于ng-repeat with track by 和 filter 以及 orderBy 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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