下面是我的插件,我试图在其中显示基于不同月份的输出类型。我想通过获取数组中的所有值来单击“保存”按钮来保存每个月的最大容量。但是当我在文本框中键入该值将重复,因为索引按列重复。
下面是代码:
JavaScript:
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.outputType=["Coal","ROM","WASTE"];
$scope.months=["JAN","FEB","MARCH","APRIL"];
$scope.values = [];
$scope.save=function(){
alert($scope.values)
}
});
HTML:
<table style="border:1px solid red;">
<thead>
<tr>
<th> </th>
<th ng-repeat="i in months"><b>{{i}}</b></th>
</tr>
</thead>
<tbody ng-repeat="item in outputType">
<tr>
<td>{{item}} </td>
<td ng-repeat="i in months">
<input type="text" ng-model="values[$index]"
placeholder="Max.Capacity">
</td>
</tr>
</tbody>
</table>
最佳答案
检查 http://plnkr.co/edit/4DUDIBoTOCI4J89FiQeM?p=preview
JS
$scope.values = {};
HTML
<input type="text" ng-model="values[item][i]" placeholder="Max.Capacity">
或
<input type="text" ng-model="values[i][item]" placeholder="Max.Capacity">
关于javascript - 使用 ng-repeat 时避免表中的列出现重复索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32782463/