Angular 2 Checkbox value with Reactive form

2019-09-15 03:34发布

问题:

i have checkbox in angular 2 reactive form i need to pass the values of these checkbox in POST api , Right now they will pass value on status. below are checkbox code it only passed the status like true or false instead of value like 1,2 something like that.

<input type="checkbox" formControlName="servicelatest"  [value]="CatArry[i].id" (change)="serviceCheckBox($event)"/>

-------------------**------------------------

<div *ngFor="let serviceType of laceDetailFrom.controls.serviceType.controls; let i=index" >
<div  [formGroupName]="i">

<input type="checkbox" formControlName="servicelatest"  [value]="CatArry[i].id" (change)="services[$event.target.getAttribute('value')]=$event.target.checked">&nbsp;{{CatArry[i].cat_name}}
                             </div>
                           </div>

回答1:

Template Side :

<input type="checkbox" formControlName="servicelatest"  [value]="CatArry[i].id" 
(change)="services[$event.target.getAttribute('value')]=$event.target.checked"/>

Component Side :

services = {};

And then use services as your selected checkboxes values