如何使用ng-repeat增加类名?

时间:2022-12-27 18:58:19

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.

我设置了一个ng-repeat来从JSON文件中推出一堆元素,但我希望每个元素都有一个增量类名.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.

我想将这个数字添加到class =“item - {{item.length}}”但我似乎无法使其正常工作。

2 个解决方案

#1


3  

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:

使用$ 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

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

#2


3  

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

如https://docs.angularjs.org/api/ng/directive/ngRepeat中所述,只需使用$ index

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

#1


3  

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:

使用$ 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

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

#2


3  

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

如https://docs.angularjs.org/api/ng/directive/ngRepeat中所述,只需使用$ index

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