chaity
chaity

Reputation: 155

Unique Identifier For Elements of a List Parsed By ng-repeat in AngularJS

I need to repeat through some list that is present in variable "tab.excelFields". I have been successfully able to do that, simple ng-repeat. The code is shown below:

<tr>
        <th ng-repeat="field in tab.excelFields" class="btn btn-danger" style="display:table-cell;" id="{{id}}">{{field}}</th>
</tr>

Notice {{id}} field. I need to put in unique values here...1,2,3 and so on

Question: How do I add unique id to each of these fields that I am displaying? Something like "id in [1,2,3]" in conjunction with my code. Two problems:-

  1. How to give "id in [1,2,3]" with "field in tab.excelFields" in ng-repeat? Can it take two arguments?

  2. My fields are not restricted to any fixed number. It can be one or one thousand. How to dynamically create a counter and add to this for "id" field?

Upvotes: 1

Views: 40

Answers (1)

YD1m
YD1m

Reputation: 5905

Use $index variable inside ngRepeat scope.

<th ng-repeat="field in tab.excelFields" class="btn btn-danger" style="display:table-cell;" id="{{$index}}">{{field}}</th>

Upvotes: 3

Related Questions