[英]How to get access to formControl element in formBuilder Array?
I'm trying to get value from mat-autocomplete but if I'm using formControlName mat-autocomplete doesn't work.我正在尝试从 mat-autocomplete 中获取价值,但如果我使用 formControlName mat-autocomplete 将不起作用。
<mat-form-field>
<mat-label>...</mat-label>
<input type="text" matInput aria-label="..."
//[formControl]="attributeListCtrl"
formControlName="attributeKey"
[matAutocomplete]="auto" [readonly]="VOForm.get('VORows').value[i].isEditable">
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn" (optionSelected)="onDBAttrSelected()">
<mat-option *ngFor="let option of attributeList" [value]="option">
{{option.nameAttribute}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
I have seen that some people recommend to use something like [formControl]= form.get('attributeKey')
but it's not working for me because there's nested array with groups in my formBuilder我看到有些人建议使用类似[formControl]= form.get('attributeKey')
的东西,但它对我不起作用,因为我的 formBuilder 中有包含组的嵌套数组
this.VOForm = this.fb.group({
VORows: this.fb.array(this.attributeList.map((val:any) => this.fb.group({
idx: new FormControl('1'),
attributeKey: new FormControl<string | Attribute>(''),
attributeValue: new FormControl(val.attribute.allValue),
action: new FormControl('existingRecord'),
isEditable: new FormControl(true),
isNewRow: new FormControl(false),
})
)) //end of fb array
});
A FormArray of formGroups is always the same: formGroups 的 FormArray 总是相同的:
You use a getter in.ts您使用吸气剂 in.ts
get VORows()
{
return this.VOForm.get('VORows') as FormArray
}
In.html在.html
<!--declare the form-->
<form [formGroup]="VOForm">
<!--use formArrayName-->
<div formArrayName="VORows">
<!--iterate over the formArray.controls using the "getter"-->
<div *ngFor="let group of VORows.controls;let i=index"
[formGroupName]="i">
<!--here you add the "inputs" using formControlName-->
<mat-form-field>
<mat-label>...</mat-label>
<input type="text" matInput aria-label="..."
formControlName="attributeKey"
[matAutocomplete]="auto"
...
<!--if we want to get reference of another formControl we can use
dot notation
-->
[readonly]="VOForm.get('VORows.'+i+'.isEditable').value">
<mat-autocomplete #auto="matAutocomplete"
[displayWith]="displayFn" ...>
<mat-option *ngFor="let option of attributeList" [value]="option">
{{option.nameAttribute}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.