如何将 ng-repeat 与过滤器和 $index 一起使用? [英] How to use ng-repeat with filter and $index?

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

问题描述

我想在 Angular 中使用 ng-repeat,而我只想输出数组的一些元素.一个例子:

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.

推荐答案

在您的代码中,过滤器适用于items"数组,而不是每个数组项,这就是它无法按您预期工作的原因.

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

相反,您可以使用 ng-show(或 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 元素,请使用 ng-if 指令:

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-repeat 与过滤器和 $index 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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