Piercey4
Piercey4

Reputation: 1358

Add element after every ng-repeat

I'm using ng-repeat but I would like to add an element after every 4th repeated element.

the repeated div:

<div class="product" ng-repeat="product in productList" ng-init="addFullScreenProduct($index, this)">

and then in my controller:

$scope.addFullScreenProduct = function(index, event)
{
        var currentProduct = "<div id='" + (index/4) + "' class='currentProduct sixteen columns'></div>";
        var product = event.srcElement;
        currentProduct = $compile(currentProduct)($scope);
        product.after(currentProduct);
};

I cannot get the "currentProduct" element to be added after the "product" element.

My Desired output:

<div class="currentProduct">...</div>
<div class="product">...</div>
<div class="product">...</div>
<div class="product">...</div>
<div class="product">...</div>
<div class="currentProduct">...</div>
<div class="product">...</div>
<div class="product">...</div>
<div class="product">...</div>
<div class="product">...</div>
<div class="currentProduct">...</div>

Any Ideas?

Upvotes: 5

Views: 2808

Answers (2)

Mikke
Mikke

Reputation: 2167

You could use ng-repeat-start and ng-repeat-end, like so:

<div ng-repeat-start="product in productList" class="product">
    {{ product }}
</div>
<div ng-repeat-end ng-if="$index % 4 == 0" class="currentproduct sixteen columns"></div>

The div.product will be repeated, with div.currentproduct being present at every fourth iteration.

Upvotes: 17

Shaunak
Shaunak

Reputation: 18018

You can use filters to show every 4th element.. like this:

<div class="product" ng-repeat="product in productList" ng-show="$index % 4 == 0"></div>

Upvotes: 0

Related Questions