我有一排输入文本框。我想引入以下功能。
a) 如果我在第一个输入中输入一个值,该值应自动复制/更新到下一个所有文本框。即从 2 到 10
b) 假设我在第 5 个框中输入/更新一个值,该值必须自动复制/更新为 6, 7,8,9,10,但不能复制/更新为 4,3,2,1。
如何以及在 angular.js 中实现此目的最简单的方法是什么?
最佳答案
获取ngChange中的值并更新后续的输入值。下面的例子。
angular
.module('exampleApp', [])
.controller('ExampleController', ExampleController);
function ExampleController() {
var vm = this;
vm.inputValues = [
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]
];
vm.updateSubsequentInputs = function(idx, row, value) {
for (var i = idx; i < row.length; i++) {
row[i] = value;
}
}
}
<!DOCTYPE html>
<html ng-app='exampleApp'>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
</head>
<body ng-controller="ExampleController as vm">
<table>
<tr ng-repeat="inputRow in vm.inputValues">
<td ng-repeat="inputValue in inputRow track by $index">
<input ng-model="inputValue" ng-change="vm.updateSubsequentInputs($index, inputRow, inputValue)">
</td>
</tr>
</table>
</body>
</html>
关于javascript - Angular.js 复制文本框数组行中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38408068/