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

标签 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> 

但是当我这样做时,它会将我的标题 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/

相关文章:

javascript - Angular ui 路由器 : stop controller from reloading on press of back button in the browser

javascript - 如何使用 JavaScript 隐藏和显示多个 Div

javascript - 无法从 jquery 的表单中获取输入值

javascript - 动态生成的图标

jquery - 捕获坐标并在其上显示 div

python - 从python中的大量数据中提取任何看起来像链接的东西

javascript - 下拉菜单的 Jquery

html - 是否可以将规则应用于标签嵌套中的特定标签?

javascript - 如何使 AngularJS 指令处于事件状态

angularjs - 如何在浏览器中忽略来自 AWS ELB 的空闲超时