$ index的ng-repeat track仅在angularjs中类似键多次存在时才显示该项目一次 [英] ng-repeat track by $index display the item only once if the similar key exists multiple times in angularjs

查看:88
本文介绍了$ index的ng-repeat track仅在angularjs中类似键多次存在时才显示该项目一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的angularjs代码中遇到[ngRepeat:dupes]:错误. 因此,我在stackoverflow上进行了搜索,找到了根据$ index声明使用track的解决方案.现在这对我有用.但是,如果多次出现相同的键,它将多次显示该项目.如果同一密钥存在多次,我只想显示一项. 这是我从视图中获取的示例代码:

I was getting [ngRepeat:dupes]: error in my angularjs code. So I searched on stackoverflow and found the solution which stated to use track by $index. This is working for me now. But, it displays the item multiple times if the same key is present multiple times. I want to display only one item if the same key exists multiple times. here is my sample code from the view :

<div ng-repeat="user in users | filter:myFilter track by $index">
</div>

这是当前输出:

出于显示这两张卡的目的,我希望代码仅显示一张卡,因为它们都是相同的. 我该怎么办?

Insted of showing these two cards, I want the code to show only one card as they both are the same. How do I do it?

推荐答案

将数组传递到函数中并创建一个没有重复值的数组 例如 myFunction(users),因此重复部分将成为 ng-repeat ="myFunction(users)中的用户" .自己编写函数.

pass array into a function and create an array without the repeating values like myFunction(users) so the repeat section will become ng-repeat="user in myFunction(users)". Write the function yourself.

这篇关于$ index的ng-repeat track仅在angularjs中类似键多次存在时才显示该项目一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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