Javascript ng repeat无法处理具有非连续数字索引的数组?
我是angular JS的新手,希望用它来处理ajax传递的json。首先,我编写了以下测试代码,以确保数据能够正确显示:Javascript ng repeat无法处理具有非连续数字索引的数组?,javascript,arrays,angularjs,Javascript,Arrays,Angularjs,我是angular JS的新手,希望用它来处理ajax传递的json。首先,我编写了以下测试代码,以确保数据能够正确显示: <head> <script src="path/to/angular.js"></script> <script> var app = angular.module('pageApp', []).config(function($interpolateProvider){
<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>
我是否遗漏了什么,或者根本没有能力处理非连续数索引数组?问题是索引2和4中的数组中有两个未定义。为了解决这个问题,可以向索引2和索引4中添加值,或者按如下所示的索引跟踪ng repeat
<span ng-repeat="tag in tags track by $index"> << tag >> </span><br>
问题在于索引2和索引4中的数组中有两个未定义。为了解决这个问题,可以向索引2和索引4中添加值,或者按如下所示的索引跟踪ng repeat
<span ng-repeat="tag in tags track by $index"> << tag >> </span><br>
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 < collectionLength; index++) {
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 < collectionLength; index++) {
您希望通过设置自己的索引来实现什么?实际数组是在通过ajax从服务器返回json时设置的,其中索引是该标记项的id。那么,如果它是
json\u编码的
,它将看起来像{3:“三”,5:“五”}
。这种索引不太需要。更常见的是有对象数组,比如[{id:3,txt:“three”},{id:5,txt:“five”}]
是的,然后我将遍历对象来设置数组。实际的json类似于{3:{word:“three”,另一个长长的属性列表},5:{word:“five”,另一个长长的属性列表},将它放在一个只包含所需属性的数组中可以提高可读性。此外,它不是直接分配。使用数组存储项通过设置actualArray[index]=$return_json['word']yes有助于重复数据消除,长度可用于计数,易于推送到数组,无法对对象进行排序等等通过设置自己的索引中的间隔,您希望实现什么?实际数组是在通过ajax从服务器返回json时设置的,其中,索引是该标记项的id。那么,如果它被json_编码
,那么它就是一个对象,看起来像{3:“三”,5:“五”}
。这种索引不太需要。更常见的是有对象数组,比如[{id:3,txt:“three”},{id:5,txt:“five”}]
是的,然后我将遍历对象来设置数组。实际的json类似于{3:{word:“three”,另一个长长的属性列表},5:{word:“five”,另一个长长的属性列表},将它放在一个只包含所需属性的数组中可以提高可读性。此外,它不是直接分配。使用数组存储项有助于重复数据消除,方法是将actualArray[index]=$return_json['word']设置为yes,并且可以使用长度对其进行计数,很容易推送到数组中,并且无法对对象进行排序。感谢您将[]更改为{}的建议。这正是我想要的。解释也很好。谢谢你建议把[]改为{}。这正是我想要的。解释也很好。