[英]Angular mat-autocomplete disable input FormControl not working
[英]Get source FormControl from mat-autocomplete
我有一个动态 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
您可以向 function 调用添加另一个参数。 您已经有了formControlName
,您可以传递formControlName [itemNumber/description]
并使用它来获取formControl
。
我想我明白了。 我移动了 ngFor 的内部。 因为我的 ngFor 看起来像这样:
<tr *ngFor="let item of salesItemsForm.get('salesItems').controls; let index=index" [formGroupName]="index">
我可以传入item
,它是从那里开始的肉汁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.