我有以下 HTML 表单:
<form fxLayout="column" [formGroup]="setPaymentForm" autocomplete="off">
<div class="input-row" fxLayout="row">
<form class="example-form" [formGroup]="setPaymentClientName">
<mat-form-field class="example-full-width">
<input matInput placeholder="Client Name" formControlName="clientName" (ngModelChange) ="getSearchedClients()" aria-label="Clients" [matAutocomplete]="auto" [formControl]="paymt_client_ctrl">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let client of filteredClients | async" [value]="client.name">
<span>{{client.name}}</span> |
<small> phone: {{client.phone}}</small>
<small> | address: {{client.address}}</small>
</mat-option>
</mat-autocomplete>
</mat-form-field>
<br>
</form>
</div>
<div class="input-row" fxLayout="row" fxLayout.lt-md="column" fxLayoutGap="20px" fxLayoutGap.lt-md="0px">
<mat-form-field class="inputField" fxFlex>
<input matInput formControlName="payment_amount" placeholder="Payment Amount" type="text">
</mat-form-field>
</div>
<div class="input-row" fxLayout="row" fxLayout.lt-md="column" fxLayoutGap="20px" fxLayoutGap.lt-md="0px">
<mat-checkbox[checked]='this.isCash' formControlName="isCash"> Cash</mat-checkbox>
</div>
<div class="modal-footer">
<button mat-raised-button name="addButton" (click)="submitPayment()" color="primary">Set Payment</button>
</div>
</form>
有两种形式,外部形式是 setPaymentForm 和内部形式,我称之为 setPaymentClientName。
我想在提交时获取这两种形式的数据,所以我做了以下功能:
submitPayment(){
this.setPaymentForm = this.fb.group({
clientName: [this.clientName, Validators.required],
payment_amount: [this.payment_amount],
isCash: [this.isCash]
});
但是一旦我打开表单,我就会收到以下错误:
PaymentsComponent.html:23 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()
});
我对 angular 6 很陌生,我习惯于使用 angularjs 构建我的 web 项目,它比 angular6 完全不同。任何帮助表示赞赏。