我有以下代码:
<div ng-repeat="list in [[1,2,3,4], [1,2,3,4]] track by $index">
<div id="{{counter}}" ng-repeat="listChild in list track by $index">
{{listChild}}
</div>
</div>
我想让
counter
是元素的数量,但要相对于整个父数组。我如何使其工作,以便div连续计数而不是作为两个单独的数组。是这样的:<div>
<div id="1">
{{listChild}}
</div>
<div id="2">
{{listChild}}
</div>
<div id="3">
{{listChild}}
</div>
<div id="4">
{{listChild}}
</div>
</div>
<div>
<div id="5">
{{listChild}}
</div>
<div id="6">
{{listChild}}
</div>
<div id="7">
{{listChild}}
</div>
<div id="8">
{{listChild}}
</div>
</div>
最佳答案
下面的代码说明了父数组中不同的数组长度。 $scope.precedingCount
跟踪滚动总数直到给定内部数组的开始
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.counter = 0;
$scope.data = [[1,2,3,4], ['a','b','c','d', 'e', 'f'], ['any', 'other', 'data', 'needed']];
$scope.precedingCount = {
0: 0
};
for(var i=1; i<$scope.data.length; i++) {
$scope.precedingCount[i] = $scope.precedingCount[i-1] + $scope.data[i-1].length
}
$scope.combinedIndex = function(oIndx, iIndx) {
return $scope.precedingCount[oIndx] + iIndx
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
<div ng-repeat="list in data track by $index" ng-init="outerIndex = $index">
<div id="{{combinedIndex(outerIndex, $index)}}" ng-repeat="listChild in list track by $index">
{{listChild}} - {{counter}} - {{outerIndex}} - {{$index}} - {{combinedIndex(outerIndex, $index)}}
</div>
</div>
<br />
<br />
<div>
The count of the previous indices: {{precedingCount}}
</div>
</div>
关于javascript - ng-repeat循环内的 Angular ng-repeat,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38248656/