最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get value from multiple checkboxes in AngularJS - Stack Overflow

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

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
Add a ment  | 

1 Answer 1

Reset to default 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" />
发布评论

评论列表(0)

  1. 暂无评论