[英]Render element in repeater conditionally
I'm tring to render items to page where I want to have five items per row. 我正在尝试将项目渲染到页面,我希望每行有五个项目。 This means I want to have div element with row class only rendered every five items in array. 这意味着我想让具有行类的div元素仅在数组中每五项呈现一次。 Here is little pseudo code: 这是一些伪代码:
<div ng-repeat="item in items">
<div class="row"> // render only every five items
<div>{{item.name}}</div>// render at all times
</div>
</div>
How is this possible to do in angular? 这怎么可能成角度地做? I already looked into ng-if and ng-show directives but these seem to hide also nested html elements which is something I do not want. 我已经研究过ng-if和ng-show指令,但是这些指令似乎也隐藏了嵌套的html元素,这是我不想要的。 This did not work for me: ng-if="$index % 5 == 0"
这对我不起作用: ng-if="$index % 5 == 0"
如果问题是class="row"
属性,则可以使用以下内容:
<div ng-class="{ 'row': $index % 5 === 0 }">
Using a directive 使用指令
Plunkr @ http://plnkr.co/edit/iNBIWFrszqfT6Y057Bck Plunkr @ http://plnkr.co/edit/iNBIWFrszqfT6Y057Bck
angular.module('treesApp', [])
.controller('treeController', function ($scope) {
console.log('AppCtrl started..');
$scope.user = 'test';
$scope.items = [];
$scope.items.push({name:"item 1"});
$scope.items.push({name:"item 2"});
$scope.items.push({name:"item 3"});
$scope.items.push({name:"item 4"});
$scope.items.push({name:"item 5"});
$scope.items.push({name:"item 6"});
$scope.items.push({name:"item 7"});
$scope.items.push({name:"item 8"});
$scope.items.push({name:"item 9"});
$scope.items.push({name:"item 10"});
$scope.items.push({name:"item 11"});
$scope.items.push({name:"item 12"});
})
.directive('sectionedList', function () {
return {
restrict: 'E',
scope: {
items: '=items',
},
template: '<div ng-repeat="item in treeItems"><div class="row"><h3>{{item.name}}</h3>' +
'<div class="col-md-2" ng-repeat="leaf in item.leaves">{{leaf.name}}</div>' +
'</row></div>',
link: function($scope) {
$scope.treeItems = [];
for (i = 0; i < $scope.items.length; ++i) {
if(i % 5 == 0) {
console.log('root: ' + i);
var len = $scope.treeItems.push($scope.items[i]);
$scope.treeItems[len-1].leaves = [];
} else {
var parent = Math.floor(i/5);
console.log('root with leave: ' + parent + ' - ' + i);
$scope.treeItems[parent].leaves.push($scope.items[i]);
}
}
console.log($scope.treeItems);
},
controller: function ($scope) {
}
}
})
-- OLD EDIT --- -旧编辑-
You could use ng-show
and pass in $index%5 == 0
as per the doc 您可以使用ng-show
并根据文档传递$index%5 == 0
Think you need a dynamic class determined by there orders: 认为您需要一个由那里的订单确定的动态类:
<div ng-repeat="item in items">
<div class="{{{true:'row', false:''}[$index%5==0]}}"> // class judge by code block.
<div>{{item.name}}</div>// render at all times
</div>
</div>
So here is what I eded up doing, to render five items per row I iterate two times trough the array and I render items conditionaly. 所以这就是我要做的事情,每行渲染五个项目,我遍历数组两次,然后有条件地渲染项目。
<div class="row" ng-repeat="item in items" ng-if="($index % 5 == 0)">
<div ng-repeat="item in items.slice($index, $index+5)">{{item.name}}</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.