作者:手机用户2702938564 | 来源:互联网 | 2023-05-25 08:05
我是angular JS的新手,想用它来处理ajax传递的json.首先,我制作了这个测试代码,以确保数据可以正确显示:
Text input:
Tag found om database: <>
但是,代码不起作用并给出以下错误:
错误:[ngRepeat:dupes]不允许在转发器中重复.使用'track by'表达式指定唯一键.Repeater:标签中的标签,Duplicate key:undefined:undefined,Duplicate value:undefined
当数组更改为连续数字的索引时,它可以工作.即
hardCode[1] = "one";
hardCode[2] = "three";
hardCode[3] = "five";
我错过了什么,或者Angular JS根本没有能力处理非连续数字索引数组?
1> 小智..:
Angular尝试确定对象是否是像对象一样的数组.一旦确定,它就会根据第一个谓词设置其trackBy属性.然后它构建其集合.
if (isArrayLike(collection)) {
collectiOnKeys= collection;
trackByIdFn = trackByIdExpFn || trackByIdArrayFn;
} else {
trackByIdFn = trackByIdExpFn || trackByIdObjFn;
// if object, extract keys, in enumeration order, unsorted
collectiOnKeys= [];
for (var itemKey in collection) {
if (collection.hasOwnProperty(itemKey) && itemKey.charAt(0) !== '$') {
collectionKeys.push(itemKey);
}
}
}
之后它使用常规for循环
for (index = 0; index
你可以在这里看到代码:
angular-ngRepeat指令
因为angular确定你正在使用像对象一样的数组,它会尝试遍历数组,逐个索引,并且它会运行到undefined.如果你必须使用带有'holes'的数组并使用ng-repeat,你应该考虑使用一个对象而不是一个数组:
var hardCode = {};
hardCode["1"] = "one";
hardCode["3"] = "three";
hardCode["5"] = "five";