如何使用ng-repeat从表中的动态创建的输入框中获取输入值?

时间:2018-06-15 01:55:27

标签: javascript html angularjs

如何使用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> 

但是当我这样做时,它会将我的Title标题绑定到该索引处的输入框的值。总结一下,我只需要与标题对应的输入框的值,该标题也在ng-repeat中动态添加。

1 个答案:

答案 0 :(得分:1)

这是一个简单的修复。在我的功能中:

$scope.gettingInputDataFromTrackables = function(){
      $scope.dataInput =[];
      console.log($scope.dataInput);
  }

每次点击时我都在初始化$scope.dataInput = [],导致数据被清除,我记录了一个空数组。这并没有打击我最长的时间。 修复

     $scope.dataInput =[];
     $scope.gettingInputDataFromTrackables = function(){          
              console.log($scope.dataInput);
          }

是的,这就是全部。我刚刚在函数范围之外声明了$scope.dataInput。简单的错误和更容易修复。

相关问题