Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Form Array push specific index

addStop() {
    const control = <FormArray>this.editForm.controls['stops'];
    control.push(this.initStop());
}

I have this code to add a "stop" at the bottom of the form array. But I want to add the new "stop" not to the last position, but one position before the last stop.

This doesn't work for example (not at all, I know that the numbers are wrong. Splice function doesn't exist at )

control.splice(2, 0, this.initStop());
like image 394
Michalis Avatar asked Jun 11 '17 21:06

Michalis


Video Answer


1 Answers

Use FormArray#insert:

control.insert(<index>, this.initStop());
like image 157
developer033 Avatar answered Sep 27 '22 21:09

developer033