2017-03-06 102 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

是的,這當然更好地解釋。這完全取決於你設計的複雜性。祝一切順利! –

相關問題