使用ng-repeat时避免表中列中的重复索引
Avoiding duplicate index in columns in table when using ng-repeat
下面是我的柱塞,我试图根据不同的月份显示输出类型。我想保存每个月的最大容量,点击保存按钮,通过在数组中获取所有的值。但是当我在文本框中输入时,值会随着索引按列重复而重复。
代码如下:
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">
保留数组的解决方案。
你需要修改input
中的ngModel <input type="text" ng-model="values[$index]" placeholder="Max.Capacity">
ng-model="values[$parent.$index][$index]"
.
下面是一个例子:
示例相关文章:
- ng init中的表达式无法使用ng repeat
- Angular js+ng repeat+字母数字索引不起作用
- 如何从 Angular ng-repeat 指令中检索数组中的某些索引
- 如何在 ng-repeat 中使用 anchorScroll() 以在索引 x 处具有开始视图
- AngularJS - ng-repeat,更新尚未定义的模型对象索引
- 如何在任何索引字段中验证 ng-repeat 中的保存按钮
- 如何在 ng-repeat 中的某个特定索引上开始一个新行
- 如何使用数据 ng-repeat获取从 A 到 Z 的索引值
- 角度.js在 ng-repeat 中获取 2d 数组中最后一个元素的索引
- 如何对一个元素进行ng样式设置's$索引由ng repeat创建
- 如何使链接内的ng-repeat可索引的SEO
- 在控制器- angular, Ionic中获取滚动后的ng-repeat索引
- 使用Angular ng-repeat获取数组的最后一个索引
- 在ng-repeat中获取ng-repeat的索引
- 获取ng-repeat中的row's索引(或id),以便对特定行执行一些操作
- 使用ng-repeat时避免表中列中的重复索引
- Angular:如何使用ng repeat迭代数组中除最终索引外的所有索引
- ng-repeat元素# 39;索引在排序时改变
- AngularJS在ng-repeat中决定索引
- 从ng-repeat中获取实际的数组索引