如何使用 ng-repeat 从表格中动态创建的输入框中获取输入值?我的 html 中有这段代码:
<table class="table table-striped" name="progressLogTable">
<thead>
<th>Title</th>
<th>Input</th>
</thead>
<tbody>
<tr ng-repeat="x in progressLog track by $index ">
<td>{{x}}</td>
<td><input type="number" ng-model="dataInput[$index]"></input></td>
</tr>
</tbody>
</table>
我需要单击按钮时生成的文本框内的值。到目前为止,这是我的 JS:
$scope.gettingInputDataFromTrackables = function(){
$scope.dataInput =[];
}
我曾尝试使用 $index 动态创建每个输入的模型,但我相信我使用不正确。我也尝试过将我的 td
生成为:
<td>{{x}}</td>
<td><input type="number" ng-model="progressLog[$index]"></input></td>
但是当我这样做时,它会将我的标题 header 绑定(bind)到该索引处输入框的值。总而言之,我只需要与标题对应的输入框的值,该标题也在 ng-repeat
中动态添加。
最佳答案
嗯,这是一个简单的修复。在我的函数中:
$scope.gettingInputDataFromTrackables = function(){
$scope.dataInput =[];
console.log($scope.dataInput);
}
每次单击它时我都在初始化 $scope.dataInput = []
导致数据被清除并且我记录了一个空数组。这并没有打击我最长的时间。 修复方法是:
$scope.dataInput =[];
$scope.gettingInputDataFromTrackables = function(){
console.log($scope.dataInput);
}
是的,就是这样。我刚刚在函数范围之外声明了 $scope.dataInput
。简单的错误和更容易的修复。
关于javascript - 如何使用 ng-repeat 从表中动态创建的输入框中获取输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50868187/