3

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 }"

4

3 回答 3

4

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>
于 2015-04-23T15:11:42.580 回答
1

use $index.

<ul>
    <li ng-repeat="tag in chartTags track by $index" class="blue-tag">
        <div class="tag " ng-class="{blue1: $index== 0 ,blue2: $index== 1 ,blue3: $index== 2  }">{{tag.term}}</div>
    </li>

</ul>

See plunker

于 2015-04-23T15:03:55.407 回答
1

first you have to track index using track by and use , to seperate class conditions and not ||

like this

<li ng-repeat="tag in chartTags track by $index" class="blue-tag">
      <div class="tag" ng-class="{blue1: $index == 0, blue2: $index == 1, blue3: $index == 2 }">{{tag}}</div>
</li>
于 2015-04-23T15:08:46.677 回答