如何使用NG重复使用过滤器和$索引? [英] How to use ng-repeat with filter and $index?

查看:95
本文介绍了如何使用NG重复使用过滤器和$索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用NG-重演角,而我只想输出数组的某些元素。一个例子:

I want to use ng-repeat in Angular, while I only want to output some elements of the array. An Example:

ng-repeat="item in items | filter:($index%3 == 0)"

但是,这是行不通的。请告诉我如何做到这一点;元素只能输出精确索引。

However, this does not work. Please tell me how to do this; only output exact index of elements.

推荐答案

在您的code,过滤器适用于物品阵列,而不是在每个数组项,这就是为什么它像您期望不起作用。

In your code, filter apply on 'items' array, not on each array item, that's why it does not work as you expect.

相反,你可以使用NG-秀(或NG-IF):

Instead, you can use ng-show (or ng-if):

<ul>
    <li ng-repeat="item in items" ng-show="$index % 3 == 0">{{item}}</li>
</ul>

请参阅: http://jsfiddle.net/H7d26

编辑:吴用,如果,如​​果你不希望添加无形的DOM元素指令:

Use ng-if directive if you do not want to add invisible dom elements:

<ul>
    <li ng-repeat="item in items" ng-if="$index % 3 == 0">{{item}}</li>
</ul>

这篇关于如何使用NG重复使用过滤器和$索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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