所以我想向表格內的表添加額外的列。添加列本身並不困難,但我不知道如何去設置他們的ng模型。將具有特定ng模型的新列添加到HTML表
這是我當前的代碼: (HTML)
<button ng-click="add()" type="button">+ column</button>
<table>
<thead id="inputtablehead">
<th class="theadlabel">(in 1.000 EUR)</th>
<th>{{startyear}}</th>
<th class="NBBCodesHeader">NBB Codes</th>
<th>Source</th>
</thead>
<tbody class="input">
<tr>
<td>number of months</td>
<td>
<input ng-model="input{{startyear}}.NumberMonths" type="text" class="{{startyear}}" required>
</td>
<td class="NBBCodes"></td>
</tr>
<tr>
<td>Fixed assets</td>
<td>
<input ng-model="input{{startyear}}.FixedAssets" class="{{startyear}}" type="text" required>
</td>
<td class="NBBCodes">20/28</td>
</tr>
<tr>
<td>Inventory</td>
<td>
<input ng-model="input{{startyear}}.Inventory" class="{{startyear}}" type="text" required>
</td>
<td class="NBBCodes">3</td>
</tr>
</table>
(JS)
angular.module("inputFields", []).controller("MyTable", function ($scope) {
$scope.startyear = new Date().getFullYear();
var nextyear = new Date().getFullYear() - 1;
$scope.add = function() {
$(".NBBCodesHeader").before("<th>"+nextyear+"</th>");
$(".input .NBBCodes").before('<td><input class='+nextyear+' type="text" required></td>');
nextyear--;
};
});
所以在我JS的<input class='+nextyear+' type="text" required>
應該成爲像<input ng-model="input'+nextyear+'.NumberMonths" class='+nextyear+' type="text" required>
明年加入的<td>
元素'月數'行。
我正在考慮給ea行添加一個NumberMonths形式的id,然後在添加列時查找id。
所以我的問題是:這是一個有效的方式來做到這一點,我將如何得到這個ID?或者我是否過度這樣做,有沒有更簡單的方法來做到這一點?