Javascript 如何使用ng repeat从表中动态创建的输入框中获取输入值?
如何使用ng repeat从表中动态创建的输入框中获取输入值?我的html中有以下代码:Javascript 如何使用ng repeat从表中动态创建的输入框中获取输入值?,javascript,html,angularjs,Javascript,Html,Angularjs,如何使用ng repeat从表中动态创建的输入框中获取输入值?我的html中有以下代码: <table class="table table-striped" name="progressLogTable"> <thead> <th>Title</th> <th>Input</th> </thead>
<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>
我曾尝试使用$index动态创建每个输入的模型,但我认为我使用的是错误的。我还尝试将我的td
生成为:
<td>{{x}}</td>
<td><input type="number" ng-model="progressLog[$index]"></input></td>
{{x}
但当我这样做时,它会将我的标题标题绑定到该索引处输入框的值。总之,我只需要与标题对应的输入框的值,标题也会动态添加到
ng repeat
中 这是一个简单的修复。在我的职能范围内:
$scope.gettingInputDataFromTrackables = function(){
$scope.dataInput =[];
console.log($scope.dataInput);
}
每次单击时,我都在初始化$scope.dataInput=[]
,结果数据被清除,我记录了一个空数组。我很长一段时间都没有受到影响修复程序是:
$scope.dataInput =[];
$scope.gettingInputDataFromTrackables = function(){
console.log($scope.dataInput);
}
是的,就这些。我刚刚声明
$scope.dataInput
在函数的作用域之外。简单的错误和更容易的修复。当您按下提交按钮时,$scope.dataInput中有什么?它是如何工作的?当我执行console.log($scope.dataInput)时
在submit函数中,它会记录一个与动态创建的输入框的值对应的空数组。很抱歉,这根本不是您所要求的,但我会让您了解角度的简单方法:将模型粘贴到您迭代的对象上。例如:ng model=“x.dataInput”
。另外,track by$index
在添加/删除任何项目时都会变得非常古怪。只需确保progressLog items中的项目有一个具有唯一值的正确id字段,并执行ng repeat=“x in progressLog track by x.id”
,即可省去大量麻烦。再说一遍,不是你要的-对不起!我不知道为什么我不想为这个“ng repeat”这样做,我为我的项目中的每一个其他人都这样做了。谢谢你提醒我!解决方案发布在下面。
$scope.dataInput =[];
$scope.gettingInputDataFromTrackables = function(){
console.log($scope.dataInput);
}