我有一个动态 matInput 对象表,如下所示:
<div formArrayName="salesItems">
<table class="app-sales-items-table">
<thead>
<th>Qty</th>
<th>Item #</th>
<th>Description</th>
<th>Weight</th>
<th>Serial No</th>
<th>Unit Price</th>
<th>Item Discount %</th>
<th>Line Total</th>
</thead>
<tbody>
<tr *ngFor="let item of salesItemsForm.get('salesItems').controls; let index=index" [formGroupName]="index">
<td>{{item.qty}}</td>
<td>
<mat-form-field class="field-full-width">
<input type="text" matInput formControlName="itemNumber" [matAutocomplete]="autoItemNumber">
</mat-form-field>
</td>
<td>
<mat-form-field class="field-full-width">
<input type="text" matInput formControlName="description" [matAutocomplete]="autoDescription">
</mat-form-field>
</td>
<td>{{item.weight}}</td>
<td>{{item.serialNumber}}</td>
<td>{{item.unitPrice}}</td>
<td>{{item.itemDiscount}}</td>
<td>{{item.lineTotal}}</td>
</tr>
</tbody>
</table>
</div>
每个 matInput 都有一个对应的 mat-autocomplete,例如:
<mat-autocomplete #autoDescription="matAutocomplete" (optionSelected)="setSelectedSalesItem($event.option.value, $event.source)">
<mat-option *ngFor="let option of (filteredSalesItems )" [value]="option">{{option.description}}
</mat-option>
</mat-autocomplete>
如您所见,在 optionSelected 方法中,我传入了 $event.source。我的真正目标是获取触发事件的 FormControl,这样我就可以在同一行中设置其他值。问题是 $event.source 是 MatAutoComplete 类型,我不确定如何从中获取(反应性)FormControl。
编辑:
堆栈闪电战: https ://stackblitz.com/edit/angular-mzryga ?file=src%2Fapp%2Fapp.component.ts