2

I'm just starting out with angular. I currently have an json array of products being displayed within an ng-repeat and am looking to add a class to every third product.

Is there a way to write a condition within the repeater that will add a class to the html element when the index%3 is 2

Thanks

2 Answers 2

5

Sure!

<div ng-repeat='item in items' ng-class='{third: $index%3==2}'>{{item}}</div>

http://plnkr.co/edit/24Ne1Cyh71INYIKxBCcc

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

1 Comment

Would it be possible to only add this class if $index%3 was equal to 2 rather than every item?
0

Yes, use the ng-class directive:

<div ng-repeat="item in items" ng-class="{ 'third-elm': $index%3 == 2 }">{{item.value}}</div>

jsFiddle: http://jsfiddle.net/bmleite/dKjz5/

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.