1

I have a small problem I would save $ index to reuse it in the next ng repeat,

I explain, I created an object array that has the same size as my json array

this is my object array

 $scope.valeur[index] = {
                    'BODY':"",
                    'TYPE':"",
                    'PARAMETRE': ""
                };

this is a picture of my json

enter image description here

So my $ index run 2 time and then 5 time and me I would like that from the first round it save the variable and it starts the next ng repeat has the previous value (here 1)

this is my ng-repeat

<div ng-repeat="(key, item) in dataApi track by $index" >
    <div u ng-repeat="(key, itemHead) in item track by $index" ng-init="firstIndex == undefined ? firstIndex = $index : firstIndex = $index+firstIndex" >
        {{firstIndex }}
    </div>
</div>

I tested with the ng-init but I have nothing concrete

i someone can help me

thanks in advance

EDIT

here is that i want, so it's with the ng-repeat of the 2nd level that I want to save the value,

1st round of ng-repeat level 2 Index starts at 0 And finished at 1

2nd round of ng-repeat level 2 Index starts at 2 (value of previous $index+1) And finished at 6

heres is a picture

enter image description here

Because I need to have a consistency with my object array

example 1st round Index = 0 $ scope.value [index] Index = 1 $ scope.value [index] 2nd round Index = 0 $ scope.value [index]

So problem because my index is 0 and not 2

Or else I would just need a variable that counts the number of times I am switching to the ng repeat lvl2

2 Answers 2

2

try using $parent.$index and you don't have to save it temporarily.

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

3 Comments

My div parent has just 2 information c is not the right way, I would like to save the 2nd level index
The question is why you need to save it? and even you have to save it, you are probably outting the ng-init in wrong place, at that point its entered in new child scope, so either use the ng-init in outer div or in any intermediate dom (if you have).
well, why you want add the last index with it? lets assume you are at 10th index and that sub array have only 3 items, so, would you going to start from 9th index? why don't you iterating simply all the nested array as a fresh iterating? exactly what you are trying for?
2

here is a working example and if you dont want to use ng-init then $parent.$index is a good option:

<html lang="en" data-ng-app="test">
<head>
    <meta charset="utf-8">
    <title>Test</title>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>
    <script type="text/javascript">

        angular.module('test',[])
                .controller('Main',function($scope)
                {
                    $scope.options = [
                    [{opt:'1'},{opt:'2'},{opt:'3'}],
                    [{opt:'1'},{opt:'2'},{opt:'3'}]
                    ];
                });
    </script>
</head>
<body ng-controller="Main">
{{'ng-repeat'}}
<div ng-repeat="data in options" ng-init="first = $index"><div ng-repeat="d in data">first:{{first}}.data:{{d.opt}}current:{{$index}}</div></div>
{{data}}
</body>
</html>

1 Comment

@Gazano you want to initialize next second ng-repeat from the first $index? right?

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.