Angular:“AbstractControl”缺少“FormControl”类型的以下属性:registerOnChange、registerOnDisabledChange、_applyFormState

Die*_*ves 4 angular angular8

我正在使用一个片段来交互 formGroup 中的所有表单控件。该代码段一开始运行良好,但突然间 angular 开始抱怨

 Object.keys(this.frmCadastroImobiliaria.controls).forEach(key => {

  const fc: FormControl = this.frmCadastroImobiliaria.get(key); //here I got an error

  if(fc.touched === true) {
    fcs.push(fc);
  }

});
Run Code Online (Sandbox Code Playgroud)

this.frmCadastroImobiliaria 是我的表单组。

错误说:

 Type 'AbstractControl' is missing the following properties from type 'FormControl': registerOnChange, registerOnDisabledChange, _applyFormState
Run Code Online (Sandbox Code Playgroud)

Ash*_*yan 5

您可以使用打字稿断言(as)。

const fc: FormControl = this.frmCadastroImobiliaria.get(key) as FormControl; 
Run Code Online (Sandbox Code Playgroud)

  • 看。“FormGroup”、“FormControl”和“FormArray”的“get”函数返回“AbstractControl”,它与“FormControl”不同。您需要使用断言并告诉编译器,返回的类型是“FormControl”而不是“AbstractControl”。 (2认同)