Angular 4 getting values from checkboxes

Viewed 23030

I am new to angular and am stuck at this pretty simple problem. I am getting data from a webservice to show a couple of checkboxes. I need a way to get the value of all of the checkboxes selected by the user. I am using material theme.

<span *ngFor="let x of maintainanceTypeList">
    <md-checkbox 
       name="{{x.maintenancetype}}" 
       value="{{x.maintenancetype}}"  
       [formControl]="CreateSocietyForm.controls['checkMaintainanceType']">
         {{x.maintenancetype}}
    </md-checkbox>
</span>
2 Answers

Instead of e.checked you can use the following:

Working example

HTML

<input class="form-check-input" type="checkbox" (change)="selectBadge($event, badge._id)">

Typescript

selectBadge (e, id) {

 if (e.target.checked) {
   this.product.badges.push(id);
 }  else {
   this.product.badges.splice(this.product.badges.indexOf(id), 1);
 }

}
Related