sacan sacan - 6 months ago 23
AngularJS Question

Get checkboxes to send value instead of boolean

I have checkboxes with values in HTML. The problem is that I want the values to store in the database and not a boolean. How do I do this?

HTML:

<div class="form-group" >
<label >Bachelor</label><br>

<input type="checkbox" id="ComputerScienceB" ng-model="vm.course.bachelor.computer" value="Computer Science"> Computer Science
<br>
<input type="checkbox" id="SystemsEngineeringB" ng-model="vm.course.bachelor.systems" value="Systems Engineering"> Systems Engineering
<br>
<input type="checkbox" id="EnvironmentalEngineeringB" ng-model="vm.course.bachelor.environmental" value="Environmental Engineering"> Environmental Engineering
<br>
<input type="checkbox" id="MechanicalEngineeringB" ng-model="vm.course.bachelor.mechanical" value="Mechanical Engineering"> Mechanical Engineering
<br>
<input type="checkbox" id="BiotechnologyB" ng-model="vm.course.bachelor.bio" value="Biotechnology"> Biotechnology
</div>
<br>


<div class="form-group">
<button type="submit" class="btn btn-primary" ng-click="vm.saveCourse()">Add Course</button>
</div>
</form>


The database inserts the hole vm.course. I am using the hole mean stack.

Answer

You need ng-true-value attribute. You can use it like this:

<input ng-true-value="Biotechnology"
  type="checkbox"
  id="BiotechnologyB"
  ng-model="vm.course.bachelor.bio">

See angular docs on input[checkbox]

Comments