【问题标题】:Accessing Data of a FormArray of FormGroup访问 FormGroup 的 FormArray 的数据
【发布时间】:2021-03-06 19:00:52
【问题描述】:

所以我有了这个 FormBuilder 设置:

surveyForm: FormGroup;
ngOnInit(): void {
     this.surveyForm = this.formBuilder.group({
       'surveyTitle': new FormControl(null),
       'surveyDescription': new FormControl(null),
       'questionsDetail': this.formBuilder.array([
         this.formBuilder.group({
           'questionType': new FormControl('mcq'),
           'question': new FormControl(null),
           'choices': this.formBuilder.array([])
         })
       ])
     });
    };

我尝试使用此代码访问选项控制:

 onAddChoice()
  {
    const control = new FormControl(null, Validators.required);
    this.surveyForm.controls.questionsDetail.control.push(control);
  }

我得到了这个错误

引用“AbstractControl”类型的属性“控件”不存在。

如果有人可以帮助我,那就太好了。

提前致谢。

【问题讨论】:

    标签: angular formarray formgroups


    【解决方案1】:

    应该是controls 而不是control

    this.surveyForm.controls.questionsDetail.controls.push(control);
    

    我建议你使用formArray控件的addControl方法,它更冗长。

    (this.surveyForm.get('questionsDetail') as FormArray).addControl(control);
    

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2021-10-15
      • 2018-09-03
      • 1970-01-01
      • 2019-05-28
      • 2019-07-23
      相关资源
      最近更新 更多