加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何删除angular2反应形式的FormArray

发布时间:2020-12-17 17:23:34 所属栏目:安全 来源:网络整理
导读:我在从ReactiveForm中删除FormArray时遇到了问题. 我有以下代码: ngOnInit() { this.survey = new FormGroup({ surveyName: new FormControl(''),sections: new FormArray([ this.initSection(),]),}); }initSection(){ return new FormGroup({ sectionTitl
我在从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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读