我从本地SQLite数据库中提取数据并使用ng-view
填充屏幕。根据返回的项目数,使用ng-repeat
重复数据。页面必须始终有6个项目。我可以将范围中的总数设为$scope.productCount
。这个数字可以在0到6之间。
我想要做的是为剩余部分额外运行ng-repeat
。意思是如果我只返回4我需要它重复两次。但是,如果我得到6回,我根本不需要它重复。
我想过做像
这样的事情ng-repeat="i in getNumber(6 - productCount) ng-show="productCount < 6"
并在我的控制器中使用它
$scope.getNumber = function(num) {
return new Array(num);
}
我不确定1)这是否正确。 2)如果我的想法会奏效。如果它是一个半身像,我不想去那个兔子洞。
答案 0 :(得分:0)
使用过滤方法怎么样。
ng-repeat='i in productCount | limitTo:4'
答案 1 :(得分:0)
<div ng-if="productCount < 6">
<div ng-repeat="data in productCount">
</div>
<div>
答案 2 :(得分:0)
(不确定我的问题是否正确)
如果项目数少于预期,您可以创建自定义过滤器来重复数组项目。
MyApp.filter("arrayItemsRepeat", function () {
return function (array, count) {
var i = 0;
while(array.length < count) {
array.push(array[i]);
i++;
}
return array;
}
});
然后在模板中使用过滤器:
ng-repeat='item in items | arrayItemsRepeat:6'