如何删除angular2反应形式的FormArray

Muh*_*fan 4 angular

我在从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());
}
Run Code Online (Sandbox Code Playgroud)

现在要删除formControl surveyName我只是做

this.survey.removeControl('surveyName');
Run Code Online (Sandbox Code Playgroud)

上面的代码对于SurveyName工作正常。但是我可以使用什么删除表单数组部分。我想用键删除整个节对象。

Pen*_*gyy 5

您应该始终使用removeControl从reactform移除formControl和整个 formArray的方法。

您需要注意的事情是,应该使用它ngIf来控制将其从reactform移除后不显示该移除的元素。

请参阅示例演示