ng-repeat注意项
x in records
(key, value) in myObj
x in records track by $id(x)
使用ng-repeat遍历一个数组,当数组中有重复的元素的时候,angularjs会报错。
错误信息:
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: user in users, Duplicate key: number:1
错误原因:ng-repeat遍历时不允许存在相同的id的对象,
采用第三种方法,将遍历的index作为id.即可避免此问题。
i in list track by i.id //自身生成一个唯一的id
i in list track by $index //利用循环的索引变量$index来使用