Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working on an AngularJS app that builds a spreadsheet out of a 2D array built by the ng-repeat function. I'm currently writing a function that will change the initial values of the array when the user enters new values on the spreadsheet. This requires me to access the point in the initial array based on its row index and column index so I can change it to the new value.

I have looked over the ng-repeat API and found that it has an $index property that allows me to check the index of the current repeated value. However, I am finding that it will only let me check the index of the value of whatever repeat loop you are inside--no other outside loops that you may also be in.

<script>

    data = [
            [A1, B1, C1],
            [A2, B2, C2],
            [A3, B3, C3]
           ]

    sheet= function($scope, $parse){
    $scope.columns = [colA, colB, colC];
    $scope.rows = data.length;
    $scope.cells = {};
    $scope.outer = data.map(function(c,row){
        return c.map(function(data, ind){
            return {
                content: data,
                model: null,
            };
        });
    });
    };
</script>


<div ng-app ng-controller="sheet">
    <table>
        <tr class="column-label">
            <td ng-repeat="column in columns">{{column}}</td>
        <tr ng-repeat="inner in outer">
            <td class="row-label" ng-repeat="data in inner">
                <div>
                    <input type="text" ng-model="data.content" value="{{data.content}}">
                </div>
            </td>
        </tr>
    </table>
</div>

I want to access the $index of outer while within the inner loop, and also be able to access the $index of the inner, too. Is there a way to do this? I plan on passing these values as parameters to the function I'm writing.

share|improve this question
    
Maybe there's a more straight-forward answer, but I would proabably just start checking out ng-repeat, perhaps you can just write you're own directive that specifically handles this case for you. A good question though. –  shaunhusain Jul 9 '13 at 16:09
1  
$parent.$index or maybe $parent().$index - One of these two should work. –  rGil Jul 9 '13 at 16:45
    
$parent.$index works. Thanks! –  user2494584 Jul 9 '13 at 16:51

1 Answer 1

up vote 2 down vote accepted

Haha, well, it was answered in the comments while I was putting together my fiddle, but here's a demonstration of it for people finding this question down the road:

http://jsfiddle.net/V8qzt/1/

<div ng-controller="MyCtrl">
  <table>
      <tr ng-repeat="row in data">
         <td ng-repeat="cell in row">
             {{cell}} ({{$index}},{{$parent.$index}})
          </td>
      </tr>
    </table>
</div>
share|improve this answer
    
I'm getting 'Duplicates in a repeater are not allowed' –  bcam Jun 16 at 13:56
1  
solved it by adding 'track by $index' (stackoverflow.com/questions/16296670/…) –  bcam Jun 16 at 13:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.