Akash khan Akash khan - 11 months ago 59
AngularJS Question

Custom counter inside ng-repeat directive

I can not initialize custom counter and reset it inside ng-repeat directive. I have a colors array with three item


My colors array length is 2. I want to bind class in my ng-repeat list colors array 0 to 2 length. When it reach max of it's length then reset it to 0 and again repeat. I can implement it other place like php,jquery etc but i can't implement it on angular view.

<!-- single cat -->
<div ng-repeat="cat in contacts|filter:search.name">
<div class="item item-divider">
{{cat.name}} <span class="badge badge-positive">{{cat.data.length}}</span>

<!-- cat contact single -->
<a class="item" href="#" ng-repeat="p in cat.data">
<h2 class="letter colors[customerCounterIndex]">{{p.name.charAt(0)}}</h2>
<span class="name">{{p.name}}</span>
<!-- end cat contact single -->
<!-- end single cat -->

Answer Source

Try this, <h2 class="letter" ng-class="colors[$index % colors.length]">