在AngularJS中交替多个CSS类

Alternate multiple CSS classes in AngularJS

假设您有一个简单的列表,可以是一个或 10 个项目。你不知道。您有 5 CSS 类 要以规则的交替模式显示。执行此操作的最佳方法是什么?

示例HTML:

<div ng-repeat="item in items | orderBy: 'Title'">
    <div class="clearfix" ng-if="$index % 3 == 0"></div>
    <div class="col-sm-4">
        <div class="tile" ng-class="{ 'purple': expression1, 'red': expression2, 'green': expression3, 'blue': expression4, 'orange': expression5 }">
            <h3 class="title">{{item.Title}}</h3>
            <div>{{item.Description}}</div>
        </div>
    </div>
</div>

CSS

.tile.purple {
    background: #5133ab;
}
.tile.red {
    background: #ac193d;
}
.tile.green {
    background: #00a600;
}
.tile.blue {
    background: #2672ec;
}
.tile.orange {
    background: #dc572e;
}

考虑到这个例子,有没有人知道确保 类 始终交替的最佳方法?

angular.module('myApp', [])
.controller('MyCtrl', function($scope) {
  $scope.items = [1,2,3,4,5,6,7,8,9,10];
  // make an array of color classes
  $scope.colors = ['purple', 'red', 'green', 'blue', 'orange'];
})
;
<div ng-app="myApp" ng-controller="MyCtrl">
  <div ng-repeat="item in items">
    <!-- use modulus operator to pick the right class per item -->
    <div class="tile" ng-class="colors[$index % colors.length]">{{item}}</div>
  </div>
</div>

Click here for live demo.