angularjsangularjs-ng-repeatangularjs-ng-class

How to use $index to change class in ng-repeat?


I have a list which is limited to 3 items, the first item needs class blue1, 2nd needs blue2, 3rd blue3.

<ul>
    <li ng-repeat="tag in chartTags" class="blue-tag">
        <div class="tag blue1" ng-class="{blue1: tag == 1 }">{{tag.term}}</div>
    </li>
    <!-- <li><div class="tag blue1">item1</div></li>
    <li><div class="tag blue2">item2</div></li>
    <li><div class="tag blue3">item3</div></li> -->
</ul>

Is there a way to write a check like
ng-class="{blue1: tag == 1 || blue2: tag == 2 || blue3: tag == 3 }"


Solution

  • I think you could solve this by no need to use track by $index as such you don't have duplicates.

    Markup

    <li ng-repeat="(k, v) in chartTags" class="blue-tag">
         <div ng-class="{blue1: k == 1 , blue2: k == 2 , blue3: k == 3 }">{{v.term}}</div>
    </li>