[英]How to add Angular Material auto complete in FormArray
I am using Angular 6. My form has a formArray
to which I Add Items on a button click.我正在使用 Angular 6。我的表单有一个
formArray
,我可以通过单击按钮向其中添加项目。 My form looks like我的表格看起来像
private initForm() {
this.orderForm = new FormGroup({
'orderItems': this.orderItems,
'customerContact': new FormControl(null),
'totalAmount': new FormControl(null)
});
}
onAddItem() {
(<FormArray>this.orderForm.get('orderItems')).push(
new FormGroup({
'itemName': new FormControl(null, Validators.required),
'itemService': new FormControl(null, Validators.required),
'itemPrice': new FormControl(null)
})
);
}
Html code html代码
<tbody formArrayName="orderItems">
<tr *ngFor="let orderItem of getControls(); let i = index" [formGroupName]="i">
<td>
<input type="text" formControlName="itemName" class="form-control" [matAutocomplete]="autoName">
<mat-autocomplete #autoName="matAutocomplete">
<mat-option *ngFor="let option of filteredOrderItems | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</td>
<td>
<input type="text" formControlName="itemService" class="form-control" [matAutocomplete]="autoService">
<mat-autocomplete #autoService="matAutocomplete">
<mat-option *ngFor="let option of filteredOrderServices | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</td>
<td>
<input class="form-control" type="text" formControlName="itemPrice" (keyup)="findTotal()">
<!-- </mat-form-field> -->
</td>
<td><button type="button" class="btn btn-danger" (click)="onDeleteIngredient(i)">Delete</button></td>
</tr>
</tbody>
Autocomplete on fileds itemName
and itemService
is not working.对文件
itemName
和itemService
自动完成不起作用。 I am not able to apply filter to these fields.我无法对这些字段应用过滤器。 I am open to suggestions even if I am able to achieve this by changing some functionality of the form.
即使我能够通过更改表单的某些功能来实现这一点,我也愿意接受建议。 Here is the part of the application Stackblitz
这是应用程序Stackblitz的一部分
You need to relate each control
inside the FormArray
to its own filteredOrderItems
array.您需要各自与
control
内部FormArray
自身的filteredOrderItems
阵列。 I answered a similar question here , please read the answer details and take a look at stackblitz reproduction.我在这里回答了一个类似的问题,请阅读答案详细信息并查看stackblitz复制。
please feel free to ask.请随时询问。 Thanks
谢谢
@Mukul Jaggi you can install angular animation
@Mukul Jaggi 你可以安装
angular animation
I have create a demo on Stackblitz .我在Stackblitz上创建了一个演示。 I hope this will help/guide to you/others.
我希望这会对您/其他人有所帮助/指导。
npm install --save @angular/animations@6.0.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.