3

I have an array like this in my controller:

$scope.myArray = [
{
    id: 1,
    name: "my object",
    options: [
        {
          id: 1,
          key: "key1",
          value: "value1"
        },
        {
          id: 2,
          key: "key2",
          value: "value2"
        }
    ]
},
{
      id: 2,
    name: "my object 2",
    options: [
        {
          id: 1,
          key: "key3",
          value: "value3"
        },
        {
          id: 2,
          key: "key4",
          value: "value4"
        }
    ]
}
]

I want to loop through this in my template in angular. This is what I have so far:

<div ng-repeat="obj in myArray">
    <input ng-model="myArray[$index].name" />

    <div ng-repeat="option in obj.options">

        /*How can I use $index again here to loop through the options array ? Wouldn't $index be for the parent loop */
        <input ng-model="obj.options[$index].value" />
    </div>
</div>

My question is that in the above inner loop where I am trying to use obj.options[$index], will this refer to the correct index of the options array or will it still think that it is using the index of the parent array ?

1
  • You are doing it wrong, use the solution provided by @MichaelFalckWedelgård Commented Oct 13, 2015 at 14:19

3 Answers 3

6

$index will always give you current ng-repeat index. To get the parent ng-repeat index use $parent.$index

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

1 Comment

Thx. Exactly what I needed.
2

Try this:

<div ng-repeat="obj in myArray">
    <input ng-model="obj.name" />

    <div ng-repeat="option in obj.options">

        <input ng-model="option" />
    </div>
</div>

In Angular when you use ng-repeat "obj in myArray" it states that "obj" is an alias for the current item being iterated over in the array.

3 Comments

I understand your answer. But I am interested in using the $index since I want to use the 2 way data binding by saving the values in scope exactly so that I can send them to server to edit. If I just do ng-model = option, it won't work when I want to resend the entire array to server to save if an edit was made. Hope this explains.
I think it will work too.beause option.value is binded to obj.options[$index] .value and object.name is binded to myArray[$index].name. ($index is always the one of the current scope)
@codegeek this solution will work with bi-directional binding 100%.
2

try this of you want to get the indexes from both ng-repeat:

<div ng-repeat="(indexParent, obj) in myArray">
   <input ng-model="myArray[indexParent].name" />
     <div ng-repeat="(index,option) in obj.options">
    <input ng-model="myArray[indexParent].options[index].value" />
</div>

Comments

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.