Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:';抽象控制&x27;类型';中缺少以下属性;FormControl';:registerOnChange,registerOnDisabledChange,\u applyFormState_Angular_Angular8 - Fatal编程技术网

Angular 角度:';抽象控制&x27;类型';中缺少以下属性;FormControl';:registerOnChange,registerOnDisabledChange,\u applyFormState

Angular 角度:';抽象控制&x27;类型';中缺少以下属性;FormControl';:registerOnChange,registerOnDisabledChange,\u applyFormState,angular,angular8,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) {

我正在使用一个片段来交互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);
  }

});
这是我的formGroup

错误显示:

 Type 'AbstractControl' is missing the following properties from type 'FormControl': registerOnChange, registerOnDisabledChange, _applyFormState

您可以使用typescript断言(
as


我将fc改为AbstractControl,显然有效。但您不能使用带有FormControl的某些方法,您的TS将给出错误。很酷,谢谢!但是为什么呢?我认为类型和接口的全部意义在于兼容类型将共享同一个接口。
FormGroup
FormControl
FormArray
get
函数返回与
FormControl
不同的
AbstractControl
。您需要使用断言并告诉编译器,返回的类型是
FormControl
,而不是
AbstractControl
const fc: FormControl = this.frmCadastroImobiliaria.get(key) as FormControl;