Angular:formarray内部formarray中未在formControl上设置值
我有一个包含一个formarray的表单,您可以动态添加值,在这种情况下,我可以通过FormControl获得值。
firstGroup = this._formBuilder.group({
dataArray: new UntypedFormArray([]), });
在第一个formarray中,还有另一个formarray也可以添加值,但是在第二个formarray中,尽管它能够通过选择框更改值,但关联的FormControl不会注册所选的值。
const line = new UntypedFormGroup({
chooseOption1: new UntypedFormControl('', Validators.required),
secondDataArray: new UntypedFormArray([]),
});
this.firstGroup.controls.dataArray.push(line);
}
addInnerLine(i: any) {
const control = <UntypedFormArray>(
this.dataArray.controls[i].get('secondDataArray')
);
control.push(this.addInnerLineData());
}
addInnerLineData() {
return new UntypedFormGroup({
qtd: new UntypedFormControl(),
});
}
我认为问题在于我定义了桌子,但是我尝试了很多事情,但我仍然无法解决。
<table
mat-table
[dataSource]="dataArray.controls[i].value.secondDataArray"
formArrayName="secondDataArray"
class="mat-elevation-z8"
>
<ng-container matColumnDef="qtd">
<th mat-header-cell *matHeaderCellDef>QTD</th>
<td mat-cell *matCellDef="let row; let index = index">
<mat-form-field appearance="outline">
<mat-select formControlName="qtd" name="qtd">
<mat-option *ngFor="let ob of exampleData" [value]="ob">
{{ ob.symbol }}
</mat-option>
</mat-select>
</mat-form-field>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayColumns"></tr>
</table>
有人可以帮我吗?
I have a form that contains a FormArray inside which you can dynamically add values and in this case I can get the values through the FormControl.
firstGroup = this._formBuilder.group({
dataArray: new UntypedFormArray([]), });
Inside this first FormArray there is another FormArray that can also be added values, but in this second one, although it was able to change the values through a selection box, the associated FormControl does not register the chosen value.
const line = new UntypedFormGroup({
chooseOption1: new UntypedFormControl('', Validators.required),
secondDataArray: new UntypedFormArray([]),
});
this.firstGroup.controls.dataArray.push(line);
}
addInnerLine(i: any) {
const control = <UntypedFormArray>(
this.dataArray.controls[i].get('secondDataArray')
);
control.push(this.addInnerLineData());
}
addInnerLineData() {
return new UntypedFormGroup({
qtd: new UntypedFormControl(),
});
}
I think the problem is in the way I'm defining the table, but I've tried so many things and I still can't solve it.
<table
mat-table
[dataSource]="dataArray.controls[i].value.secondDataArray"
formArrayName="secondDataArray"
class="mat-elevation-z8"
>
<ng-container matColumnDef="qtd">
<th mat-header-cell *matHeaderCellDef>QTD</th>
<td mat-cell *matCellDef="let row; let index = index">
<mat-form-field appearance="outline">
<mat-select formControlName="qtd" name="qtd">
<mat-option *ngFor="let ob of exampleData" [value]="ob">
{{ ob.symbol }}
</mat-option>
</mat-select>
</mat-form-field>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayColumns"></tr>
</table>
Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过多次尝试并阅读并查看了很多示例,我找到了解决方案。
首先,我创建了一个辅助阵列。
当我添加新的内线时,我将AuxDataSource转换为新的MattabledataSource
,最后我将html上的表更改为:
我已更新 stackblitz
After many attempts and read and see lot of examples, i've found the solution.
First i've created an aux array.
When I add a new inner line I turn the auxDataSource in a new MatTableDataSource
And finally I changed the table on HTML to this:
I've updated the Stackblitz