最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add element after every ng-repeat - Stack Overflow

programmeradmin2浏览0评论

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 = $pile(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?

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 = $pile(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?

Share Improve this question edited Apr 4, 2014 at 20:34 Piercey4 asked Apr 4, 2014 at 20:13 Piercey4Piercey4 1,35811 silver badges10 bronze badges 1
  • why not just use filter? – Shaunak Commented Apr 4, 2014 at 20:18
Add a ment  | 

2 Answers 2

Reset to default 17

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.

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>
发布评论

评论列表(0)

  1. 暂无评论