Angular2窗体中的TypeScript编译问题 [英] Issue with TypeScript compilation in Angular2 Forms

查看:66
本文介绍了Angular2窗体中的TypeScript编译问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Angular2中定义了一个表单,如下所示:

I have defined a form in Angular2 like that:

    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.

现在,在我的组件中,我正在使用:

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;
}

但这给了我更多错误:

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;

对我有用.我认为这只是一种解决方法.

It works for me. I thinks it's only a workaround.

这篇关于Angular2窗体中的TypeScript编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆