Angularjs 间隔和循环的Ng重复中的Ng重复
我想在ng repeat中进行ng repeat,并在第一次ng repeat的每3项之后显示第二次ng repeat的结果。当第二次ng重复的数据不足时,我想重新开始,直到第一次ng重复完成 阵列:Angularjs 间隔和循环的Ng重复中的Ng重复,angularjs,ng-repeat,Angularjs,Ng Repeat,我想在ng repeat中进行ng repeat,并在第一次ng repeat的每3项之后显示第二次ng repeat的结果。当第二次ng重复的数据不足时,我想重新开始,直到第一次ng重复完成 阵列: 项目=[ “第1项”、“第2项”、“第3项”、“第4项”、“第5项”、“第6项”、“第7项”、“第8项”、“第9项”、“第10项” ] 条=[ “BAR1”、“BAR2” ]一种解决方案是创建一个新的数组,并每隔三个索引插入条元素: var newArray = []; items.forEa
项目=[
“第1项”、“第2项”、“第3项”、“第4项”、“第5项”、“第6项”、“第7项”、“第8项”、“第9项”、“第10项”
]
条=[
“BAR1”、“BAR2”
]
一种解决方案是创建一个新的数组,并每隔三个索引插入条
元素:
var newArray = [];
items.forEach(function (item, index) {
if (index % 3 === 0) {
// bars.shift() will remove the first element of bars and return it
newArray.push(bars.shift());
}
newArray.push(item);
});
然后您可以在
newArray
上ng repeat
,如果您在ng repeat中对项目进行迭代之前建立项目列表,可能会更简单,如下所示:
var myApp=angular.module('myApp',[]);
控制器('MyCtrl',['$scope',函数($scope){
$scope.items=[
“第1项”、“第2项”、“第3项”、“第4项”、“第5项”、“第6项”、“第7项”、“第8项”、“第9项”、“第10项”
];
$scope.bar=[
“BAR1”、“BAR2”
];
$scope.allItems=函数(){
var-arr=[],j=0;
$scope.items.forEach(函数(item,idx){
如果(idx>0&&idx%3==0){
arr.push($scope.bar[j%$scope.bar.length]);
j+=1;
}
arr.push(项目);
});
返回arr;
};
}]);代码>
-
{{item}}
如果您想要纯基于模板的内容:
<div ng-repeat="item in items">
<div>{{item}}</div>
<div ng-if="($index+1) % 3 === 0">{{bars[ (($index+1) / 3 - 1)%(bars.length) ]}}</div>
</div>
{{item}}
{{bar[($index+1)/3-1)%(bar.length)]}
演示:这就是我需要的。非常感谢你。