typescript - Angular2 Forms 中的 TypeScript 编译问题

标签 typescript angular angular2-forms

我在 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/

相关文章:

html - 以 Angular 6 旋转框架内的矩形图像

angular - 使用 Typescript 在 Angular 中忽略 GET 请求中未定义的属性集

javascript - 表单中的最小长度验证不起作用

javascript - 如何在打印之前等待 HTML 文档加载/渲染(纯 JavaScript)

typescript - TypeScript 中的交集和合并类型 `{ foo } & { bar }` 和 `{ foo, bar }` 之间的区别

node.js - Nestjs-i18n 翻译 Handlebars 模板不起作用

Angular2 ReactiveFormsControl : how to bind radio buttons?

typescript - 静音/忽略来自 TypeScript tsc 的 TS2307 错误

excel - 如何在 Angular 中将 Excel 模板的设计作为对象导入

angular - 将 formControlName 与类似 <span> 的东西一起使用