0

I have a ul li structure like below and need to add blank li after every second element, how its possible with ng-repeat in angular js.

    <ul>
<li>content</li>
<li>content</li>
<li class="filler"></li>
<li>content</li>
<li>content</li>
</ul>

    <ul>
<li ng-repeat="item in items">item.content</li>
</ul>
5
  • items.push( //push what you want...). Commented Sep 11, 2015 at 10:35
  • Does this filler element contain anything? Or it is just some kind of spacer? Maybe instead of modifying DOM tree, you should try using styles? I.e defining a style for li:nth-child(2n) Commented Sep 11, 2015 at 10:40
  • The filler using for a kind of spacer. So need to be render with class filler. Commented Sep 11, 2015 at 10:44
  • @gaurav Unfortunately I can;t push anything to the json data, because its its huge one and its reusable for another areas. Commented Sep 11, 2015 at 10:47
  • ng-class="{filler:!$index%3}" Commented Sep 11, 2015 at 11:30

1 Answer 1

1

One way is to use ng-repeat-start with ng-repeat-end

<li ng-repeat-start="item in items">{{item.content}}</li> 
<li ng-repeat-end ng-if="$index % 2 == 1">Filler</li>
<!--  OR -->
<li ng-repeat-end ng-if="$odd">Filler</li>

If the filler is purely a style element you can use css to manage it without using ng-repeat-end

li:nth-child(odd):after{
  display:block;
  content: ' ';
  /** other filler rules **/
}

DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

What %2 does ? in ng-if="$index %2 == 1".
@YOU ohhhh, I never even knew there was a $odd. I knew $last and $first but not that one...perfect thnx
@gauravbhavsar it is a modulo operation. you should look that up in a javascript arithmetic operators reference
@charlietfl ahh, got it, result should be 1 after dividing by 2. Many thanks.

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.