How to place values ​​for flags with ng-model?

I have two checkboxes in the form, now the user can select one or both and send data to the server. But I'm not sure how I will bind both values ​​to $scope.ng-model when the user publishes 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> 
+5
source share
2 answers

two-way data binding corners works as follows: Initializing form data in the controller area

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

And in your opinion:

 <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 flags, the value of your model will also instantly change. So all you have to do is deal with your $scope.form in your controller.

+1
source

I don’t know if this is what you are looking for, but why not try your own model:

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

therefore, in .js code you can send and receive the "myModel" model as an object, and the values ​​will be true or false.

 ... var data = $scope.myModel ... $scope.myModel = data; 
0
source

Source: https://habr.com/ru/post/1236717/


All Articles