javascript - AngularJS increment value for id attributes -
i have html looks this:
<tr ng-repeat="x in y"> <td> <div ng-attr-id="{{getid()}}"></div> </td> <td> <div ng-attr-id="{{getid()}}"></div> </td> <td> <div ng-attr-id="{{getid()}}"></div> </td> </tr>
i want have id starts 1 first <td>
element , increments 1 each <td>
element.
by doing in controller:
$scope.getid = function () { counter++; return counter; }
i
10 $digest() iterations reached. aborting!
you can use $index this:
<tr ng-repeat="x in y track $index"> <td> <div ng-attr-id="{{$index + 1}}"></div> </td> </tr>
for nested loops can define new track value , combine $index
or static 3 elements can write this:
<tr ng-repeat="x in y track $index"> <td> <div ng-attr-id="{{$index*3 + 1}}"></div> </td> <td> <div ng-attr-id="{{$index*3 + 2}}"></div> </td> <td> <div ng-attr-id="{{$index*3 + 3}}"></div> </td> </tr>
Comments
Post a Comment