问题
I have defined a form in Angular2 like that:
this.form = this._formBuilder.group({
password: ['',Validators.required],
passwordRepeat: ['',Validators.required]
});
where
public form:ControlGroup
Which is fine, as:
_formBuilder = FormBuilder.group(controlsConfig: {
[key: string]: any;
}, extra?: {
[key: string]: any;
}): modelModule.ControlGroup
it returns ControlGroup
.
Now, in my component I am using:
this.user.password = this.passwordEditForm.controls.password.value;
Which throws me compilation error of:
error TS2339: Property 'password' does not exist on type '{ [key: string]: AbstractControl; }'.
Seems like a bug. Any ideas on how I can overcome this issue? I've tried doing so:
export interface FormControlGroup extends ControlGroup{
password:any;
}
But this gives me even more errors:
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; }'.
回答1:
replace the following :
this.user.password = this.passwordEditForm.controls.password.value;
with
this.user.password = this.passwordEditForm.controls['password'].value;
It works for me. I thinks it's only a workaround.
来源:https://stackoverflow.com/questions/35987494/issue-with-typescript-compilation-in-angular2-forms