添加重复键对象时如何更新 ng-repeat 列表中的元素
How to update element in ng-repeat list when duplicated key object gets added
<table>
//thead[...]
<tr ng-repeat="user in users track by user.username">
<td>{{user.username}}</td>
...
</tr>
</table>
每当我添加一个元素($scope.users.push(newUser);
),其键在ng-repeat列表中重复时,我都会收到错误:
Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: user in users, Duplicate key: username
我对此没意见,但我希望使用新对象的值更新旧对象。换句话说,我希望它更新或替换。
我在谷歌上找不到任何帮助,所以我迷路了。
使用 .
splice 而不是 .push 来添加/删除(基本上替换)数组中的项目。
很烦人,因为它可能是这样的(更像是伪代码,因为我真的不知道你的大部分代码):
for (var i =0; i <=length_of_array; i++){
if (array[i].admin == new_element.admin)
{
array[i] = new_element;
new_element =null;
}
}
if (new_element != null)
{
array.push(new_element)
}
就像我说的,没有任何更多的上下文,这基本上是我推荐的,我不是角度专家,但除非有一个我不知道的特定角度解决方案,否则这是同时的替代选择。
相关文章:
- 如何使用ng repeat中的选定输入更新我的对象
- 通过使用ng if更新ng repeat加载程序
- Flexslider-通过ng repeat更新图像-宽度不正确
- AngularJS ng repeat没有't在LocalStorage更改时更新(使用store.js)
- 如何只更新ng repeat中的部分元素
- 使用括号表示法添加到关联数组时,ng-repeat 不会更新
- 角度 1.5 不更新 ng-repeat
- AngularJS ng-repeat在更新时不起作用
- 在角度材料对话框数据输入/推送后更新ng-repeat
- AngularJS - ng-repeat,更新尚未定义的模型对象索引
- 添加重复键对象时如何更新 ng-repeat 列表中的元素
- AngularJS ng-repeat在新数据进入时不更新视图
- 从另一个 Angular 控制器更新 ng-repeat
- AngularJS ng-repeat稍微慢一点更新
- 嵌套的 ng-repeat 不会更新对象
- 如何在 ng-repeat中更新过滤器功能
- 角度ng-repeat不更新,当使用jQuery内部指令调用Angular 方法时
- 如何使用 Angular 更新 NG-Repeat 中的列表
- 推送到数组不会通过ng repeat指令更新视图列表
- 当ng-repeat更新时,HTML表没有更新