3

I have an array a=[1,2,3,4,5,6].

Using ng-repeat on this array, I am creating 6 divs.

Please refer to this plunker

Is there any way to add one more div after each row. So after 3 divs I want to add one extra div.

I looked into this example. but they are creating a child div. is it possible to create a sibling div in ng-repeat

0

4 Answers 4

8

Let's try ng-repeat-start & ng-repeat-end.

<div class="example-animate-container">
  <div class="square" ng-repeat-start="friend in a">
   {{$index}}
  </div>
  <div ng-if="$index % 3 == 2" ng-repeat-end>
    extra div
  </div>
</div>

Please note that ng-repeat-start directive is included since angular 1.2.1.

plnkr demo

ng-repeat: repeat a series of elements of "one" parent element

<!--====repeater range=====-->
  <Any ng-repeat="friend in friends">
  </Any>
<!--====end of repeater range=====-->

ng-repeat-start & ng-repeat-end: using ng-repeat-start and ng-repeat-end to define the start point and end point of extended repeater range

<!--====repeater range start from here=====-->
<Any ng-repeat="friend in friends" ng-repeat-start> 
  {{friend.name}}
</Any><!-- ng-repeat element is not the end point of repeating range anymore-->

<!--Still in the repeater range-->
<h1>{{friend.name}}</h1>

<!--HTML tag with "ng-repeat-end" directive define the end point of range -->
<footer ng-repeat-end>
</footer>
<!--====end of repeater range=====-->
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain how ng-repeat-start and ng-repear-end works?
+1 for this one but I think your second example using ng-repeat-startis wrong. Where you have <Any ng-repeat="friend in friends" ng-repeat-start> should be <Any ng-repeat-start="friend in friends"> instead. It didn't work for me otherwise.
1

The ng-repeat-start and ng-repeat-end syntax was introduced for this exact purpose. See the documentation http://docs.angularjs.org/api/ng/directive/ngRepeat

So you'll do something like:

<div ng-init="a = [1,2,3,4,5,6]">

<div class="example-animate-container">
  <div class="square" ng-repeat-start="friend in a">
   {{$index}}
  </div>
  <div ng-repeat-end>Footer</div>
</div>

Comments

0

You can use a empty div as a container for your repeat. And then only render the child element's as squares (or what ever you want). This way, your extra div will be a sibling and not a child.

See my example: http://plnkr.co/edit/xnPHbwIuSQ3TOKFgrMLS?p=preview

Comments

0

take a look at this plunker here, could it be your solution? i don't understand really well your problem because i think it's better if instance your array in an external js file, but other than that take a look here plunker

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.