Pregunta

I set up an ng-repeat to rollout a bunch of elements from a JSON file however I would like each element to have a incremental class name .item-1, .item-2, .item-3.

<div ng-controller="itemCtrl">
  <div class="item" ng-repeat="item in items">
    <span class="item-{{ item.length }}"></span>
  </div>
</div>

I would like to add the number to class="item-{{ item.length}}" but I can't seem to get this working.

¿Fue útil?

Solución

I don't know what is the benefit of generating dynamic class names like this. A class should mean a class of similar objects.

It's still possible with $index:

<div ng-controller="itemCtrl">
  <div class="item" ng-repeat="item in items">
    <span class="item-{{ $index }}"></span>
  </div>
</div>

https://docs.angularjs.org/api/ng/directive/ngRepeat

Otros consejos

As documented on https://docs.angularjs.org/api/ng/directive/ngRepeat just use $index

<div ng-controller="itemCtrl">
  <div class="item" ng-repeat="item in items">
    <span class="item-{{ $index }}"></span>
  </div>
</div>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top