我正在尝试使用具有 4 个级别的嵌套数据结构创建一个“无限滚动”页面,通过 ng-repeats 显示,如下所示:
Category 1 - Item - Sub Item 1 - Activity A - Activity B - Activity C - Sub Item 2 - Activity D Category 2
I've been able to get an accurate index of a given item via the following function in my scope, but I'm not sure how to apply that to the filter on the various ng-repeats to cut off at say the 5th item (Activity B) in my list above.
$scope.itemIndex = function(item) {
var count = 0;
if($scope.training[0] && $scope.training[0].Assigned.length > 0) {
count++;
if(item === $scope.training[0].Assigned) {
return count;
}
for (var n = 0; n < $scope.training[0].Assigned.length; n++) {
count++;
if(item === $scope.training[0].Assigned[n]) {
return count;
}
for (var r = 0; r < $scope.training[0].Assigned[n].requirements.length; r++) {
count++;
if(item === $scope.training[0].Assigned[n].requirements[r]) {
return count;
}
for(var a = 0; a < $scope.training[0].Assigned[n].requirements[r].activities.length; a++) {
count ++;
if(item === $scope.training[0].Assigned[n].requirements[r].activities[a]) {
return count;
}
}
}
}
}
}
我已经尝试了范围内全局计数器的各种组合,并通过子级传递 $index ,但一直无法找到有效的解决方案。以可以动态更改计数的方式限制单个总计数的嵌套重复的最佳方法是什么?
最佳答案
您是否尝试过使用 ng-if 将每个列表的 ng-repeat 限制在可接受的范围内。
在您所说的索引上使用 ng-if。
检查https://docs.angularjs.org/api/ng/directive/ngIf更深入地了解如何使用 ng-if 指令
希望这有帮助
关于javascript - 限制嵌套 Angular ng-repeat 数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596756/