Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript. FormGroup FormArray - remove only one element object by value. Angular 2 4

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    } ] 
like image 712
user7337867 Avatar asked Oct 12 '17 10:10

user7337867


People also ask

How do I remove an item from FormArray?

You can manually clear each FormArray element by calling the removeAt(i) function in a loop.

What is the difference between FormGroup and FormArray in Angular?

Whereas FormGroup represents an entire form or a fixed subset of a form's fields, FormArray usually represents a collection of form controls that can grow or shrink.

What is form array in angular?

Angular FormArray is an inbuilt class that tracks the value and validity state of an array of FormControl, FormGroup, or FormArray instances. For example, if one of the controls in a FormArray is invalid, an entire array becomes invalid. Angular FormArray is one of the three fundamental building blocks used to define forms in Angular, ...

How do I get the string name of formarray in typescript?

It accepts the string name of FormArray registered in the FormGroup created in the TypeScript. The selector of FormArrayName is formArrayName . Find the HTML template code for the above form group.

What is @angular formarray?

Angular FormArray is a bit like FormGroup, and it’s used in a very similar way, the difference being that it’s used as an array that envelops around an arbitrary amount of FormControl, FormGroup, or even other FormArray instances. FormArray is a class of @angular/forms module.

How to remove an index from a form array in angular?

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


1 Answers

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

this.images.removeAt(this.images.value.findIndex(image => image.id === 502)) 
like image 56
AT82 Avatar answered Sep 19 '22 03:09

AT82