html5 - Angular 2 Reactive Forms Checkbox - Bind Bool to Number -
i have form model indicators represented checkboxes. model value true/false in form object json. value convert boolean number, 1/0 respectively. there smart way this?
example code:
@component template: ` <form [formgroup]="myform" (ngsubmit)="save(myform.value)"> <input type="checkbox" id="mytoggle" formcontrolname="mytoggle"/> </form> ` export class mycomponent implementes oninit{ private myform:formgroup; mytoggle: number; constructor(private _fb:formbuilder) {} ngoninit() { this.myform = this._fb.group({ mytoggle: [0] }); }
hopefully above demonstrating i'm trying set type of "mytoggle" number. initializing form setting default 0 , correctly leaving checkbox unchecked. however, updating checkbox checked set form value true instead of 1. want updated 1. via this question see there options converting booleans numbers. however, i'm unsure of how implement reactive form model.
formcontrol
has registeronchange
method, allows specify callback executed after every change.
having example can access control let ctrl = this.myform.controls["mytoggle"]
(i'd prefer create manually) , can sth ctrl.registeronchange(() => ctrl.patchvalue(ctrl.value ? 1 : 0));
.
Comments
Post a Comment