2017-03-06 59 views
0

如何从angularjs中的复选框获取动态数据?我尝试使用下面的代码,但得到的错误:Angularjs从复选框中获取动态数据

TypeError: Cannot read property '0' of undefined

HTML

<tr ng-repeat="procForAcnts in procForAcnt"> 
        <td>{{procForAcnts[0]}}</td> 
        <td><input type="checkbox" ng-model="procNums[$index]" ng-change="test()" value="{{procForAcnts[2]}}"> {{procForAcnts[4]}}</td> 
        <td>{{procForAcnts[3]}}</td> 
        <td>{{procForAcnts[1]}}</td> 
        <td>{{procForAcnts[5]}}</td> 
        <td>{{procForAcnts[2]}}.00</td> 
        <td><center>--</center></td> 
        <td>{{procForAcnts[7]}}</td> 
        </tr> 

JS

$scope.test = function() { 
var len= $scope.procForAcnt.length; 
alert(len); //working 
for(i=0; i<len; i++){ 
alert($scope.procNums[i]); 
} 
} 
+0

检查您的变量'procForAcnts'是否定义 –

+0

您能否提供“procForAcnt” – Mamun

回答

0

虽然,你还没有发布完整的代码在这里。我可以建议你发送参数给你的测试方法。

替换:

<td><input type="checkbox" ng-model="procNums[$index]" ng-change="test()" value="{{procForAcnts[2]}}"> {{procForAcnts[4]}}</td> 

有了:

<td><input type="checkbox" ng-model="procNums[$index]" ng-change="test(procNums[$index])" value="{{procForAcnts[2]}}"> {{procForAcnts[4]}}</td> 

然后在您的测试方法获得的价值作为参数:

$scope.test = function(selectedValue) { 
    alert(selectedValue); 
}; 

请让我知道,如果有帮助!

+0

的示例数据谢谢,但得到了解决方案http://stackoverflow.com/questions/14514461/how-do-i-bind-to -list-of-checkbox-values-with-angularjs –

+0

是的,这当然更好地解释。这完全取决于你设计的复杂性。祝一切顺利! –