我在使用Angular 2表单时遇到了一些麻烦,我在项目中成功实现了一些表单,当我尝试添加该表单时,我从Angular Cli中得到了错误:
Type 'FormGroup' is not assignable to type 'typeof FormGroup'.
有人遇到过这个吗?
这是我的组件代码:
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],
})
}
这是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>
最佳答案
我猜你把=
和:
弄混了。
将您的myAdressForm
定义更改为此:
export class AddressBookComponent {
myAddressForm : FormGroup;
...
}
键入方式(
myAdressForm = FormGroup
),会将myAdressForm
属性分配给FormGroup
的类类型关于Angular 2 RC-4表单, 'FormGroup'类型不能分配给 'typeof FormGroup'类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38438466/