Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS - ng-repeat and loop conditions

Hey i have this piece of code:

<div ng-repeat="i in values">
{{i}}
</div>

It works but i would like to put extra conditions inside the loop something like:

<div ng-repeat="i in values">
{{i}}
if(i !== 0){
<div></div>
}
</div>

How can i achieve this?

like image 309
itsme Avatar asked Jan 25 '14 14:01

itsme


2 Answers

Use ng-if (or ng-show):

<div ng-repeat="i in values">
   <div ng-if="i !== 0"></div>
</div>

Attached to the element, this will decide whether to display it or not.

Documentation for ng-if can be found here.

However, if you want to only do something if you are looping the first or last item, you can use $first and $last properties of ng-repeat as well. These are documented with ng-repeat. And can be used like this:

<div ng-repeat="i in values">
   <div ng-if="$first"></div>
</div>
like image 103
Davin Tryon Avatar answered Nov 05 '22 08:11

Davin Tryon


Use the ng-if statement

<div ng-repeat="i in values">
   {{i}}
   <div ng-if="i !== 0"></div>
</div>
like image 39
StarsSky Avatar answered Nov 05 '22 09:11

StarsSky