Search code examples
javascriptangularjsangular-ngmodel

AngularJS checkbox ng-model as an array


My HTML is

    <label>
  <input type="checkbox" name="Communities" ng-model="formData.User.Communities" value="1" id="YOURROLE_0">
  COACH</label>
<br>
<label>
  <input type="checkbox" name="Communities" ng-model="formData.User.Communities" value="2" id="YOURROLE_1">
  ATHLETE</label>
<br>
<label>
  <input type="checkbox" name="Communities" ng-model="formData.User.Communities" value="3" id="YOURROLE_2">
  PARENT</label>

How can I specify the HTML/Javascript that I get the model as below. I need the checkboxes (checked ones) as an array populated to the array.

.controller('formController', function($scope) {

        $scope.formData = {
            "User": {
                "Communities": [1, 2, 3]
            }
        };

    });

Solution

  • Checklist Model is the answer

    http://vitalets.github.io/checklist-model/