source

스크립트를 입력합니다.FormGroupFormArray - 값별로 요소 객체를 하나만 제거합니다.앵귤러 24

ittop 2023. 8. 26. 00:03
반응형

스크립트를 입력합니다.FormGroupFormArray - 값별로 요소 객체를 하나만 제거합니다.앵귤러 24

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

다음과 같은 방법으로 새 요소를 추가합니다.this.images.push(new FormControl(new ImageCreateForm(this.imageResponse.id)));

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

내 수업:

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

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

이미지를 추가할 때는{{ formGroup.value | json }}다음과 같습니다.

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

이미지를 제거하려고 합니다(예: 이미지만 해당).id=502에서▁from서formGroupPOST 요청 양식을 보낼 때 이전.가능합니까?사용해 보았습니다.resetthis.images.reset({"id":image.id});서 . . . .image은 그은것입니다.ImageResponse물건.

결과:{"images": [ null, null, null ]}하지만 난 원해요:

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

FormArray클래스는 인덱스를 가져갑니다.인덱스를 모르는 경우 다음과 같은 해결 방법을 수행할 수 있습니다.

// 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)

에서, 각진반성 응형태,,formArray에는 라방법있습다니이라는 .removeAt()삭제할 인덱스를 전달하여 사용할 수 있습니다.

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

FormGroup에서 특정 Key 값을 제거하려면 다음 코드를 사용할 수 있습니다.

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

언급URL : https://stackoverflow.com/questions/46707026/typescript-formgroup-formarray-remove-only-one-element-object-by-value-angul

반응형