我在 Angular2 中定义了一个这样的表单:
this.form = this._formBuilder.group({
password: ['',Validators.required],
passwordRepeat: ['',Validators.required]
});
在哪里
public form:ControlGroup
这很好,因为:
_formBuilder = FormBuilder.group(controlsConfig: {
[key: string]: any;
}, extra?: {
[key: string]: any;
}): modelModule.ControlGroup
它返回 ControlGroup
。
现在,在我的组件中我正在使用:
this.user.password = this.passwordEditForm.controls.password.value;
这会引发以下编译错误:
error TS2339: Property 'password' does not exist on type '{ [key: string]: AbstractControl; }'.
似乎是一个错误。关于如何克服这个问题的任何想法?我试过这样做:
export interface FormControlGroup extends ControlGroup{
password:any;
}
但这给了我更多的错误:
error TS1206: Decorators are not valid here.
app/form.component.ts(30,9): error TS2322: Type 'ControlGroup' is not assignable to type 'FormControlGroup'.
Property 'password' is missing in type 'ControlGroup'.
app/form.component.ts(37,61): error TS2339: Property 'password' does not exist on type '{ [key: string]: AbstractControl; }'.
最佳答案
替换以下内容:
this.user.password = this.passwordEditForm.controls.password.value;
与
this.user.password = this.passwordEditForm.controls['password'].value;
它对我有用。我认为这只是一种解决方法。
关于typescript - Angular2 Forms 中的 TypeScript 编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35987494/