Issue with TypeScript compilation in Angular2 Forms

会有一股神秘感。 提交于 2019-12-10 11:19:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!