1

I have the following code to push a new item to the very end of the form array:

(<FormArray>this.invoicesForm.get('invoices')).push(this.fBuilder.group({
            id: null,
            issueDate: [null, Validators.required], // Only fill in and set the required validator for issue date if a custom date has already been chosen
            invoiceIssuedStateCode: [0],
            canEditIssueDate: [true],
            chooseDate: [false],
            issued: [false],
            paidStatus: [0],
            reference: [null],
            creditNotes: this.buildCreditNotes([]),
            lineItems: this.buildNewLineItems(this.lineItemsRecord.lineItems)
        }));

However I need to push this item as the second last item in the array. How would I go about this?

2 Answers 2

5

Instead of pushing, you could splice it:

Splice

it would work this way:

(<FormArray>this.invoicesForm.get('invoices')).controls
    // splicing instead of pushing
    .splice(-2, 0, this.fBuilder.group({
            // your fb config
        }));

notice the -2 (index -2 equals length - 2), and the 0. The -2 says where to insert or delete, the 0 that you are deleting 0 iterables, and the object you pass is then inserted (the fb.group()).

In short: you are targeting index -2 (previous to last item), you are telling it to delete 0 items, and then you are pushing at that index a new item (your formGroup object). That's how splice works!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but I get a strange error saying "Property 'splice' does not exist on type 'FormArray'"
OHHHHH I get what's happening. don't splice to FormArray... splice to FormArray.controls
0

You can use the javascript function 'splice' over the array.

 var index =  (<FormArray>this.invoicesForm.get('invoices')).length-1;

  fruits.splice(index, 0, this.fBuilder.group({
            id: null,
            issueDate: [null, Validators.required], // Only fill in and set the required validator for issue date if a custom date has already been chosen
            invoiceIssuedStateCode: [0],
            canEditIssueDate: [true],
            chooseDate: [false],
            issued: [false],
            paidStatus: [0],
            reference: [null],
            creditNotes: this.buildCreditNotes([]),
            lineItems: this.buildNewLineItems(this.lineItemsRecord.lineItems)
        }));

It will add the item as second last item in the array.

2 Comments

Thanks but I get a strange error saying "Property 'splice' does not exist on type 'FormArray'"
@AndrewJuniorHoward Can you please try fruits.insert(index,this.fBuilder.group({..........) instead of splice. You have to just provide the index and new item to the insert function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.