7

I have an ng-repeat within a ng-repeat. I am tyring to obtain the index at both levels but am not able to :

<tbody>
    <tr ng-repeat="element in body">
      <td ng-repeat="h in header" style="width:{{h.width}}px">
        <div col="{{$parent.$parent.$index}}" row="{{$parent.$index}}">{{element[h.column]}}</div>
      <td>
    </tr>
  </tbody>  

here is a plnkr too. http://plnkr.co/edit/dGoN43HzQefVpCsp2R3j

The ISSUE is that value of "col" never gets populated. It does not capture the index. Can anyone assist

1
  • do you want to set outer loop index to col and inner loop index to row? Commented Mar 27, 2014 at 6:06

1 Answer 1

12

You do not have to use $parent, just use $index to get index of inner loop and use indexOf() function of array to get index of outer loop...

HTML

<tr ng-repeat="element in body">
  <td ng-repeat="h in header" style="width:{{h.width}}px">
    <div col="{{body.indexOf(element)}}" row="{{$index}}">{{element[h.column]}}</div>
  <td>
</tr>

UPDATE

Actually using ng-init would much better to get index of outer loop... here you can easily set rowIndex to current index at every turn and use it in inner loop...

  <tbody>
    <tr ng-repeat="element in body" ng-init="rowIndex = $index">
      <td ng-repeat="h in header" style="width:{{h.width}}px">
        <div col="{{$index}}" row="{{rowIndex}}">{{element[h.column]}}</div>
      <td>
    </tr>
  </tbody> 

here is updated PLUNKER

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please suggest, how I can achieve the same if I am getting the outer loop list from a function call like <tr ng-repeat="element in getBodyElements()">
@SoftEngi it should be same. Why do you think it should be different then this or what is your problem exactly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.