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

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

问题描述

我收到 [ngRepeat:dupes]: 我的 angularjs 代码错误.所以我在 stackoverflow 上搜索并找到了声明使用 $index 跟踪的解决方案.这现在对我有用.但是,如果多次出现相同的键,它会多次显示该项目.如果多次存在相同的键,我只想显示一项.这是我的视图示例代码:

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

这是当前的输出:

为了显示这两张卡片,我希望代码只显示一张卡片,因为它们是相同的.我该怎么做?

解决方案

将数组传递给函数并创建一个没有重复值的数组像 myFunction(users) 这样重复部分将变成 ng-repeat="user in myFunction(users)".自己编写函数.

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>

here is the current output :

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?

解决方案

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.

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

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