簡體   English   中英

如何刪除angular2反應形式的FormArray

[英]how to remove the FormArray in angular2 reactive forms

我在從ReactiveForm中刪除FormArray時遇到了問題。

我有以下代碼:

ngOnInit() {
  this.survey = new FormGroup({
    surveyName: new FormControl(''),
    sections: new FormArray([
      this.initSection(), 
    ]), 
  });      
}

initSection(){
  return new FormGroup({
    sectionTitle : new FormControl(''),
    sectionDescription : new FormControl(''),
  });
}

addSection(){
  const control = <FormArray>this.survey.controls['sections'];
  control.push(this.initSection());
}

現在我要刪除formControl surveyName

this.survey.removeControl('surveyName');

以上代碼適用於surveyName。 但是我可以使用什么來刪除表單數組部分。 我想用鍵刪除整個section對象。

您應該始終使用removeControl從reactiveform中刪除formControl和整個 formArray。

你需要注意的事情是你應該使用ngIf控制在從被動ngIf中移除后不顯示被移除的元素。

看示例演示

使用removeAt()方法從formarray中刪除元素

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM