我对棱角JS很陌生,我想用它来处理ajax传递的json。首先,我编写了这个测试代码,以确保数据能够正确显示:
<head>
<script src="path/to/angular.js"></script>
<script>
var app = angular.module('pageApp', []).config(function($interpolateProvider){
$interpolateProvider.startSymbol('<<').endSymbol('>>');
});
</script>
</head>
<body ng-app="pageApp">
<div class="container" ng-controller="tagging">
Text input:<textarea ng-model="description" ng-change="checkTag()" required></textarea><br>
Tag found om database: <span ng-repeat="tag in tags"> << tag >> </span><br>
</div>
</body>
<script>
//$http is not used now but will be used in the actual ajax handling.
function taggingController($scope, $http) {
$scope.tags = [];
$scope.checkTag = function () {
var hardCode = [];
hardCode[1] = "one";
hardCode[3] = "three";
hardCode[5] = "five";
angular.forEach(hardCode, function(value, key) {
console.log($scope.tags[key]);
$scope.tags[key] = value
});
};
};
app.controller("tagging", taggingController);
</script>但是,代码不起作用,并给出了以下错误:
错误:ngRepeat:不允许中继器中的dupes重复。使用“track”表达式指定唯一的键。中继器:标签中的标签,重复键:未定义:未定义,重复值:未定义
当数组更改为连续数字的索引时,它可以工作。即
hardCode[1] = "one";
hardCode[2] = "three";
hardCode[3] = "five";我是否遗漏了任何东西或角度JS根本没有能力处理非连续的数字索引数组?
发布于 2015-01-26 05:34:38
角试图确定该对象是否为类似于对象的数组。一旦确定,它将根据第一个谓词设置其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 < collectionLength; index++) {您可以在这里看到代码:角-ngRepeat指令
因为角确定您使用的是一个类似于对象的数组,所以它尝试迭代数组、索引,然后运行到未定义的数组中。如果必须使用带有“孔”的数组并使用ng-重复,则应该考虑使用对象而不是数组:
var hardCode = {};
hardCode["1"] = "one";
hardCode["3"] = "three";
hardCode["5"] = "five";发布于 2015-01-26 05:31:08
问题在于索引2和4的数组中有两个未定义的值。
<span ng-repeat="tag in tags track by $index"> << tag >> </span><br>https://stackoverflow.com/questions/28145155
复制相似问题