Angular 角度:';抽象控制&x27;类型';中缺少以下属性;FormControl';:registerOnChange,registerOnDisabledChange,\u applyFormState
我正在使用一个片段来交互formGroup中的所有表单控件。这个片段一开始运行得很好,但突然间,angular开始抱怨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) {
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;