I have complicated form with FormArray with many different FormGroups. Inside one type of formGroup I have control with value of percent. I need show and edit it value as numbers in 1 - 100, and save all form with quota value 0.1 - 1 (post to server). I can do some change in store function, but I have to check all items in FormArray if it is formGroup that I need to change. Also I need to update it when I get value to patchValue to form. Can I differ view and value in control in reactive form?
1 Answer
simple way, using [ngModel] and (ngModelChange)
<input [ngModel]="form.get('percent').value"
(ngModelChange)="this.form.get('percent').setValue((+$event)/100)
[ngModelOptions]="{standalone:true}">
Well, this is for a fomControl, but as you has a formArray, I supouse you need has a function that return the formControl
getControl(index:number)
{
return (this.form.get('nameOfArray') as FormArray)
.at(index)
.get('percent')
}
and use [ngModel]="getControl(i).value" and (ngModelChange)="getControl(i).setValue((+$event)/100)
remember that the formControl and formArray exist even there are not a input
1 Comment
kisarin
Thank you! I get what I want - the way I can use in other formGroups. stackblitz.com/edit/angular-reactive-forms-rbglar
numbers1-100 and want to store aspercentageindb? Do you only have 1 field of this kind? And also please include yourformArraycode