Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post values for checkboxes with ng-model?

I have two checkboxes in the form now user can select one or both and post the data to server, But i am not sure,how i will bind both values to $scope.ng-model when user post the data ?

main.html

<div class="col-md-8 col-md-offset-2">
    <label class="checkbox">
        <input type="checkbox" ng-value="'uploadPrc'" ng-model="prcModel">Upload PRC
    </label>
    <label class="checkbox">
        <input type="checkbox" ng-value="'uploadPrt'" ng-model="prtModel">Upload PRT
    </label>
</div>
like image 262
aftab Avatar asked Nov 09 '22 01:11

aftab


1 Answers

angulars "two way data binding" works like this: Initialize your form data on the controller scope

$scope.form = {
  prcModel: false,
  prtModel: false
}

And in your view:

<div class="col-md-8 col-md-offset-2">
    <label class="checkbox">
        <input type="checkbox" ng-value="'form.prcModel'" ng-model="form.prcModel">Upload PRC
    </label>
    <label class="checkbox">
        <input type="checkbox" ng-value="'form.prtModel'" ng-model="form.prtModel">Upload PRT
    </label>
</div>

If you change the value of your checkboxes the value of your model will instantly change as well. So all you need to do is deal with your $scope.form in your controller.

like image 53
Andre Kreienbring Avatar answered Nov 15 '22 06:11

Andre Kreienbring