Angular - Form Array push specific index
Asked Answered
P

1

21
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());
Pocky answered 11/6, 2017 at 21:39 Comment(0)
C
45

Use FormArray#insert:

control.insert(<index>, this.initStop());
Concatenate answered 11/6, 2017 at 21:41 Comment(1)
And if index is not given for some reason, Infinity is also a valid value. It will add it to the end just like pushArrogance

© 2022 - 2024 — McMap. All rights reserved.