jeudi 22 janvier 2015

AngularJS: tracking which checkbox is checked

In an HTML form, I need to track which checkboxes get checked so I can insert it in my query object that is to be posted to the backend.


I have the following three checkboxes in an array in the $scope:



$scope.typeCheckBoxes = [
{label: "A", val: true},
{label: "B", val: false},
{label: "C", val: false}
];


and this below is my template for rendering the checkboxes in the form:



<span ng-repeat="chk in typeCheckBoxes">
<input type="checkbox" ng-model="chk.val" id="chk.label" ng-change="addType(this)" />
<label>{{chk.label}}</label>
</span>


and in my controller, I tried to create the function addType() to add the checked checkbox to my query object:



$scope.queryObj = {
types: []
};

$scope.addType = function(e) {
var selectedBox = angular.element(e.id); // not working
if (selectedBox) {
$scope.queryObj.types.push(selectedBox);
}

};


However, I am not sure how I can get the id of the checked checkbox. your help is appreciated very much.





Aucun commentaire:

Enregistrer un commentaire