2014-06-30 148 views
0

我JSON格式加載從數據庫中的數據,這樣的:($ scope.fees):動態NG-模型

{"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"} 

這需要(如網格)顯示在一個表,其中行和列不固定。此代碼現在工作對我來說:

<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"應該居然規定的name屬性:data-ng-model="fees.{{bat.bid}}_{{obj.id}}"但這不起作用。有沒有解決方案來實現這個目標?提前致謝。

編輯:如果有更好的解決方案來完成這項工作,我可以更改JSON格式。目前的格式是batch<underscore>category: fees

回答

2

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

+0

謝謝。有用。我沒有嘗試+'_'+ –

0

檢查這個Demo。這顯示瞭如何從JSON對象動態添加模型。可以幫助你。

就像變量鍵在JavaScript中使用[]在NG-模型作爲爲支架值必須是你的對象鍵