如何删除angular2反应形式的FormArray

前端之家收集整理的这篇文章主要介绍了如何删除angular2反应形式的FormArray前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在从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控制在从被动变形中移除后不显示被移除的元素.

见样品demo.

猜你在找的Angularjs相关文章