반응형
스크립트를 입력합니다.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서formGroup
POST 요청 양식을 보낼 때 이전.가능합니까?사용해 보았습니다.reset
this.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
반응형
'source' 카테고리의 다른 글
포크()와 vfork()의 차이점은 무엇입니까? (0) | 2023.08.26 |
---|---|
src 원본 이미지를 찾을 수 없을 때 "이미지를 찾을 수 없음" 아이콘을 자동으로 숨기는 방법 (0) | 2023.08.26 |
MySQL--필드 목록의 알 수 없는 열 (0) | 2023.08.26 |
비 PowerShell 명령의 출력을 억제하시겠습니까? (0) | 2023.08.26 |
두 원소가 동일한지 여부 검정 (0) | 2023.08.26 |