this.cardForm = this.fb.group({
number: ['', Validators.compose([Validators.required, this.validateCardNumber])],
holderName: ['', Validators.required],
expiry: this.fb.group({
expirationMonth: ['', Validators.required],
expirationYear: ['', Validators.required],
}),
正如你所看到的,我創建了一個名爲expiry
嵌套表格組。到我的模板:
<form [formGroup]="cardForm" novalidate="novalidate" (ngSubmit)="onSave()">
<div class="form-group">
<label for="cardnumber">Card number</label>
<input
type="text"
formControlName="number"
placeholder="Card number"
name="cardnumber"
class="input-transparent form-control">
</div>
...
<div class="form-group" formGroupName="expiry">
<label for="expirationmonth">Expiration month</label>
<select2 id="default-select"
name="expirationmonth"
[data]="months$ | async"
[width]="250"
[options]="select2Options">
</select2>
<label for="expirationyear">Expiration year</label>
<select2 id="default-select2"
name="expirationyear"
[data]="years$ | async"
[width]="250"
[options]="select2Options">
</select2>
</div>
我得到這個錯誤:
ERROR Error: formGroup expects a FormGroup instance. Please pass one in.
Example:
<div [formGroup]="myGroup">
<input formControlName="firstName">
</div>
In your class:
this.myGroup = new FormGroup({
firstName: new FormControl()
});
任何想法?
,我們需要更多的代碼,如果是'this.cardForm = this.fb.group( ...)叫? – n00dl3
爲select2組件添加代碼 – yurzui
@ n00dl3,它在構造函數的組件上調用。 – Jordi