20

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?

0

2 Answers 2

52

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>
Sign up to request clarification or add additional context in comments.

7 Comments

ng-if is a better solution than ng-show because ng-if modifies the DOM. ng-show only shows/hides the element.
@RaviH yes, that is why the example uses ng-if. But, they are both valid solutions based on what you are trying to achieve.
@DavinTryon hey thank you , do you know man how can i put an incremental var in this same loop ? :P i would like to show a div inside loop only if increment%3 === 0 for example but i can't find out how :(
@sbaaaang There is one built in to ng-repeat: $index. You've also got $odd and $even to do things like alternate rows.
yeah, that should do it.
|
9

Use the ng-if statement

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

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.