0

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 = {};
});

1 Answer 1

3

You 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" />
Sign up to request clarification or add additional context in comments.

9 Comments

You are okay with binding one parameter to all checkboxes, but I believe he is trying to do set different values for each checkbox where ng-value is not works for checkbox...
ng-value is working correctly here. Check the produced HTML, then input value is indeed equal to $index.
can you provide plunker for this?
Thx a lot..it works !! just as a suggestion I am getting data now like - { 1=true, 3=true} can I improve this too ..means {Student_email: <>, checked: <>} something like this
@TechSolvr just change ur inbox with this <input type="checkbox" ng-model="student.checked"/> and you will see student.checked true or false...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.