【发布时间】:2020-07-28 03:28:35
【问题描述】:
请让我知道我应该怎么做才能使 formArray 的每一行都不会更改其他行中已选择值的值?
请注意:动态添加控件并在过滤器中正确获取值是有效的。只有当我更改它时,它才会更改其他下拉列表。谢谢。
我有一个工作 FormArray,只需按一下按钮,就会为其动态添加控件。这些控件有两个下拉菜单,根据第一个下拉菜单的不同,第二个下拉菜单的值会发生变化。
然而当我添加下一个动态控件并更改第二个下拉列表的值时,它也会重置第一个已选择值的值。我正在为这些使用材料<mat-select>。
我的 HTML:
<form [formGroup]="allocationForm" autocomplete="off">
<div fxLayoutAlign="start center" id="allocationsDiv" class="container row">
<mat-card-title style="text-align: left;">Models
<button mat-raised-button class="mat-raised-button-custom" style="padding: 3px;"
(click)="onAddCars()">Add Cars</button></mat-card-title>
<ng-container formArrayName="allocationsArray">
<div *ngFor="let ctrl of allocationControls.controls; let i = index" [formGroupName]="i">
<mat-form-field appearance="outline" style="width: 250px;padding: 3px;">
<mat-label>Car Model</mat-label>
<mat-select formControlName="carModel" id="carModel"
(selectionChange)="filterCarModel($event, i)">
<mat-option *ngFor="let models of distinctModels" [value]="models">{{models}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field appearance="outline" style="width: 350px;padding: 3px;">
<mat-label>Car Submodel</mat-label>
<mat-select formControlName="subModel" id="subModel" (selectionChange)="filtersubModel($event)">
<mat-option *ngFor="let subModel of subModels" [value]="subModel.itemValue">
{{subModel.displayValue}}
</mat-option>
</mat-select>
</mat-form-field>
</ng-container>
</div>
</form>
onAddCars() 方法将新控件推送到 allocationsArray
我的 ts:对于 filterCarModel 方法
public filterCarModel(e:any,ind:any)
{
let index = <FormArray>this.allocationForm.controls['allocationsArray'];
var carList = Array.from(new Set(this.productsDTO.filter(x => x.productType === e.value).sort().values()));
this.subModels = Array<Productdropdown>();
carList.forEach(productLists => {
const tempItem = new Productdropdown();
tempItem.displayValue = productLists["carDescription"]
tempItem.itemValue = productLists["carCode"]
this.subModels.push(tempItem);
});
}
如何过滤第二个控件值,以不更改代码中 allocationsArray 第一行中已选择的值。 使用角度 7x
onAddCars() 是这样的..
public onAddCars(){
this.allocationControls.push(this.formBuilder.group({carModel: '',subModel:''}))
}
您能否告诉我应该怎么做才能使 formArray 的每一行都不会更改其他行中已选择值的值?任何帮助表示赞赏。请注意:动态添加控件并在过滤器中正确获取值有效。只有当我更改它时,它才会更改其他下拉列表。谢谢。
【问题讨论】:
标签: angular typescript angular-material angular-material2 formarray