dearn44
dearn44

Reputation: 3422

How should I bind values of an object inside an ng-repeat?

I have a complex object that I wish to display and edit in my page.

More specifically for my actual case, I have an object that has a <string, array> pair, where array is actually a collection of objects with various <string, int> pairs.

I try to iterate over this complex structure like this:

      <div data-ng-repeat="(key, table) in TestData">
            <h3>{{key}}</h3>

            <table class="responsive-table">                    
                <tbody>
                    <tr data-ng-repeat="row in table">
                        <td data-ng-repeat="(label, value) in row" data-title="label">
                            <div>
                                <input data-ng-model="TestData[key][?][label]" data-ng-readonly="isEditable(label)" data-ng-change="show()" />                                    
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>

        </div>

There seems to be no way for me to find what value I should use in my model because I cant get my exact item in the structure inside the second repeat.

Is there a way to achieve what I want?

Upvotes: 2

Views: 310

Answers (3)

Johannes Jander
Johannes Jander

Reputation: 5020

Rewrite the show() function:

$scope.show= function (row) {
   console.log("Test: ", row);
}

and change the ngChange-Handler to data-ng-change="show(row);. This way, you can update your model in the show-function.

Upvotes: 2

basilikum
basilikum

Reputation: 10528

You should not bind directly to primitives. What you can do is use $index to bind to the array item instead:

<div ng-repeat="item in test track by $index">
   <input data-ng-model="test[$index]" data-ng-change="show()" />
</div>

Upvotes: 2

Ced
Ced

Reputation: 1301

Inside the ng-repeat there exists an own scope you can use

$scope.show= function () {
   console.log("Test: ", $scope.$parent.test);
};

to receive the result you expect

Upvotes: 0

Related Questions