Sunish Menon
Sunish Menon

Reputation: 162

dynamic ng-model inside ng-repeat

I am loading data from database in JSON Format, like this: ($scope.fees):

{"1_0":"2000","1_1":"1900","1_2":"1800","1_3":"1700","1_4":"1600","1_5":"1500","1_6":"1400","1_7":"1300","2_0":"4000","2_1":"3900","2_2":"0","2_3":"0","2_4":"0","2_5":"0","2_6":"0","2_7":"0"}

This needs to be displayed in a table (like grid), in which rows and columns are not fixed. This code works for me now:

<tbody data-ng-repeat="obj in courses"><!-- Courses JSON -->
<tr><th>{{obj.name}}</th></tr>
<tr data-ng-repeat="bat in obj.batches"><!-- Each course contains Batches -->
    <td>{{bat.bname}}</td>
    <td data-ng-repeat="obj in categories"><!-- Columns based on categories -->
      <input type="text" name="{{bat.bid}}_{{obj.id}}" data-ng-model="fees.1_0" />
    </td>
</tr>

data-ng-model="fees.1_0" should be actually as provided for the name attribute: data-ng-model="fees.{{bat.bid}}_{{obj.id}}" but this doesn't work. Is there any solution to get this working? Thanks in advance.

Edit: I can change the JSON format if there is a better solution to get this done. The current format is batch<underscore>category: fees

Upvotes: 0

Views: 155

Answers (2)

Satyam Koyani
Satyam Koyani

Reputation: 4274

Check this Demo. This shows how to attach model dynamically from JSON object.May this will help you.

Just like variable keys in javascript use [] in the ng-model as the as bracket value must be your object key

Upvotes: 0

noj
noj

Reputation: 6759

Try data-ng-model="fees[bat.bid + '_' + obj.id]"

Upvotes: 2

Related Questions