这是我的问题:
- I'm using ng-repeat to create input fields from an array. [a, b, c,...]
- I have a button that lets you add more blank input fields.
- After clicking the save button I want to be able to create a new array from the input fields.
所以我正在考虑使用一个迭代的 ng-model (item-1, item-2, item-3,...)
来跟踪每个输入,然后在我点了保存。但这需要我将每个输入与一个不是来自 [a, b, c,....] 的新变量绑定(bind)。
所以我要问的是如何使用 AngularJS 执行此伪代码。
<input ng-repeat="letter in letters"
set-value="letter.value"
bind-to-model="someNewVariable">
最佳答案
看这个例子:
HTML :
<div class="addcontend">
<div ng-repeat="item in inputs track by $index">
<input ng-model="inputs[$index]" ng-value="item" />
<button ng-click='getVal(item)'>get input value</button>
</div>
</div>
<button ng-click="addfield()">Add field</button>
<button ng-click="log()">log</button>
Controller :
app.controller('cntr',function($scope){
$scope.inputs = ['a', 'b'];
$scope.addfield = function(){
$scope.inputs.push('')
}
$scope.getVal = function(v){
alert(v);
}
$scope.log = function() {
console.log($scope.inputs)
}
})
关于javascript - 有没有办法将输入绑定(bind)到值并使用不同的 ng-model 进行跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45581525/