Angular 2 Checkbox value with Reactive form

邮差的信 提交于 2019-12-12 03:28:57

问题


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



来源:https://stackoverflow.com/questions/43154299/angular-2-checkbox-value-with-reactive-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!