100
this.formGroup = this.formBuilder.group({
    images: this.fb.array([])
});

I add new element in this way: this.images.push(new FormControl(new ImageCreateForm(this.imageResponse.id)));

get images(): FormArray {
    return <FormArray>this.formGroup.controls.images;
}

My classes:

export class ImageCreateForm {
    id: number;
    constructor(id: number) {
        this.id = id;
    }
}

export class ImageResponse {
    id: number;
    url: string;
}

When I added images, then my {{ formGroup.value | json }} is:

"images": [
   {
    "id": 501
   },
   {
    "id": 502
   },
   {
    "id": 503
   }
]

I want to remove images (for example only image with id=502) from formGroup before when I send my form POST request. Is it possible? I tried use reset method, but this remove all elements: this.images.reset({"id":image.id});. Where image it is a ImageResponse object.

Result: {"images": [ null, null, null ]}, but I want:

"images": [
   {
    "id": 501
   },
   {
    "id": 503
   }
]

4 Answers 4

239

FormArray class has removeAt which takes the index. If you do not know the index, you can do a workaround:

// findIndex returns -1 if not found
// `removeAt` wraps around from the back if given negative
const index = this.images.value.findIndex(image => image.id === 502)
if(index !== -1) this.images.removeAt(index)
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct way to remove from a FormArray. Don't use formArray.controls[i].splice - that will remove the control from the array but the control values will remain on the values array of the FormArray control.
23

In Angular Reactive Forms, formArray has a method called removeAt(). You can use it by passing an index that you want to delete:

delete(index){
    this.array.removeAt(index)
}
``

Comments

0

There are two different cases where you may don't want to send value for a formcontrol.

  1. In case you have a value for that and want to remove before post

    this.form.removeControl('controlname');      
    
  2. If you model validation is failing after posting but you still want that control in your form. In this case you can only initialize this field value as undefined. This way it will not post along with formdata values.

    this.formBuilder.group({    
       name: ['', [Validators.required],
       imageUrl: [undefined, []],
    });
    

Comments

0

if you want to remove a value for a particular Key from the FormGroup, You can use following code -

this.formGroupF.controls[value].patchValue(null)

1 Comment

That way you are setting null for the value field of the form. But if you really want to remove an element completely, you can do it this way. this.form.removeControl('value'); I often use this for unit tests that have lines like these this.form.get('value')?.value

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.