Angular JS - How to add extra DIV in ng-repeat

浪尽此生 提交于 2019-12-23 17:51:14

问题


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


回答1:


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=====-->



回答2:


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>



回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/22907982/angular-js-how-to-add-extra-div-in-ng-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!