typescript Angular 2 RC-4 表单,“FormGroup”类型不可分配给“typeof FormGroup”类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38438466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:40:05  来源:igfitidea点击:

Angular 2 RC-4 forms, Type 'FormGroup' is not assignable to type 'typeof FormGroup'

angularformstypescript

提问by MatWaligora

I am having some trouble with Angular 2 forms, I succesfully implemented a few forms in my project and when I try to add this one, i get and error from my Angular Cli: Type 'FormGroup' is not assignable to type 'typeof FormGroup'.

我在使用 Angular 2 表单时遇到了一些问题,我成功地在我的项目中实现了一些表单,当我尝试添加这个表单时,我的 Angular Cli 出现错误: Type 'FormGroup' is not assignable to type 'typeof FormGroup'.

Anyone met with this ?

有人遇到过这个吗?

Here is my component code:

这是我的组件代码:

import { Component } from '@angular/core';
import {AngularFire} from "angularfire2/angularfire2";

import {DataService} from "../../shared/data.service";
import {FormGroup, FormBuilder, Validators} from "@angular/forms";

@Component({
  moduleId: module.id,
  selector: 'tm-address-book',
  templateUrl: 'address-book.component.html',
  styleUrls: ['address-book.component.css'],
  providers: [DataService]
})
export class AddressBookComponent {
  myAddressForm = FormGroup;
  addressbook: Array<any>;

  constructor(private af: AngularFire,
          private formBuilder: FormBuilder,
          private dataService: DataService) {
  af.database.list('Addressbook').subscribe(address => {
    this.addressbook = address;
    console.log(this.addressbook);
  });

  this.myAddressForm = formBuilder.group({
    'companyName': ['', Validators.required],
    'street': ['', Validators.required],
    'city': ['', Validators.required],
    'postCode': ['', Validators.required],
  })
}

And here is the html form code:

这是 html 表单代码:

<form [formGroup]="myAddressForm" (ngSubmit)="onSubmit()">
            <div class="form-group">
              <input type="text" class="form-control" formControlName="companyName" name="companyName" placeholder="Nazwa firmy">
            </div>
            <div class="form-group">
              <input type="text" class="form-control" formControlName="street" placeholder="Ulica">
            </div>
            <div class="form-group">
              <input type="text" class="form-control" formControlName="city" name="city" placeholder="Miejscowo??">
            </div>
            <div class="form-group">
              <input type="text" class="form-control" formControlName="postCode" name="postCode" placeholder="Kod pocztowy">
            </div>
            <button type="submit" class="btn btn-success waves-effect waves-light" data-toggle="collapse" data-target="#collapse1">
              Dodaj
            </button>
          </form>

回答by Poul Kruijt

I guess you messed up =with :.

我猜你搞砸=:

Change your myAdressFormdefinition to this:

将您的myAdressForm定义更改为:

export class AddressBookComponent {

   myAddressForm : FormGroup;
   ...
}

The way you typed it (myAdressForm = FormGroup), will assign the property myAdressFormto the class type of FormGroup

您键入它的方式(myAdressForm = FormGroup),将财产分配myAdressForm给类类型的FormGroup