Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular FormArray Insert

I am trying to update a specific value on an formArray using insert(). I initialize form on ngOninit() like this:

this.myForm = this.fb.group({
    exercises: this.fb.array([]),
    type: new FormControl(),
    day: new FormControl(),
    sets: this.fb.array([]),
    reps: this.fb.array([]),
});

I have an input where on change i call the below function but when i try to insert a new value with the array's index I get the value pushed.

onSetsChange(sets, index){
  var setsFormArray = <FormArray>this.myForm.controls.sets;
  setsFormArray.insert(index, new FormControl(sets));
}

The html code is below:

<div class="col-md-3">
  <select class="form-control input-sm" (change)="onSetsChange($event.target.value, exerciseIndex)">
     <option *ngFor="let set of sets; let i = index;">{{set}}</option>
  </select>
</div>

The exerciseIndex I pass is from a loop that it doesn't show up.

What i am doing wrong and the value it's not updating? Thank you

like image 670
oMpamparos Avatar asked Jun 01 '26 01:06

oMpamparos


1 Answers

onSetsChange(sets, index){
    var setsFormArray = <FormArray>this.myForm.controls.sets;
    (setsFormArray.at(index)).patchValue(sets);
    console.log(index);
    console.log(setsFormArray);
}

this is another solution I figured out. Thank you

like image 82
oMpamparos Avatar answered Jun 02 '26 15:06

oMpamparos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!