Following is my code in which I am trying to get values of multiple checkboxes selected, but some how its not working. Let me know what I am doing wrong here, BTW the list is dynamic and not going to contain only 3 items in an array.
My Plnkr - PLNKR CODE
HTML -
<table>
<tr ng-repeat="student in studentList">
<td ng-bind="$index + 1"></td>
<td ng-bind="student.email"></td>
<td ng-bind="student.fname"></td>
<td ng-bind="student.lname"></td>
<td>
<input type="checkbox" ng-model="studcheck" ng-value="$index" />
</td>
</tr>
</table>
{{studcheck}}
CONTROLLER CODE -
var myApp = angular.module('myApp', []);
myApp.controller('mainCtrl', function($scope){
$scope.studentList = [
{email: '[email protected]', fname: 'Test1', lname: 'Last1' },
{email: '[email protected]', fname: 'Test2', lname: 'Last2' },
{email: '[email protected]', fname: 'Test3', lname: 'Last3' },
];
$scope.studcheck = {};
});
Following is my code in which I am trying to get values of multiple checkboxes selected, but some how its not working. Let me know what I am doing wrong here, BTW the list is dynamic and not going to contain only 3 items in an array.
My Plnkr - PLNKR CODE
HTML -
<table>
<tr ng-repeat="student in studentList">
<td ng-bind="$index + 1"></td>
<td ng-bind="student.email"></td>
<td ng-bind="student.fname"></td>
<td ng-bind="student.lname"></td>
<td>
<input type="checkbox" ng-model="studcheck" ng-value="$index" />
</td>
</tr>
</table>
{{studcheck}}
CONTROLLER CODE -
var myApp = angular.module('myApp', []);
myApp.controller('mainCtrl', function($scope){
$scope.studentList = [
{email: '[email protected]', fname: 'Test1', lname: 'Last1' },
{email: '[email protected]', fname: 'Test2', lname: 'Last2' },
{email: '[email protected]', fname: 'Test3', lname: 'Last3' },
];
$scope.studcheck = {};
});
Share
Improve this question
asked May 24, 2015 at 7:54
Tech SolvrTech Solvr
5051 gold badge9 silver badges26 bronze badges
1 Answer
Reset to default 3You cannot use ng-model
multiple times on the same model, but here just specify the key and it should be fine:
<input type="checkbox" ng-model="studcheck[$index]" ng-value="$index" />
If you want to update the student
model to know if it is checked or not, you could try using ng-change
:
<input type="checkbox" ng-model="studcheck[$index]" ng-value="$index" ng-change="student.checked = !student.checked" />