在另一个FormArray ho中拥有一个FormArray,我可以访问第二个FormArray吗?
这就是我在我的组成部分中所拥有的:
registrationForm = new FormGroup({
registrations: new FormArray([this.patchRegistrationValues()])
});
patchRegistrationValues(): FormGroup {
return new FormGroup({
//some other FormControls
setDate: new FormArray([this.patchSetDate()]),
});
}
get registrations(): FormArray {
return this.registrationForm.get('registrations') as FormArray;
}如何访问setDate来填充它?我试过了,但没有用:
get setDate(): FormArray {
return this.registrations.get('setDate') as FormArray;
}现在有人怎么做吗?我真的需要索引吗?
更新
关于最近的回复,这是我的html:
<thead>
<tr>
<!-- TODO: add translation to table -->
<th>Project</th>
<th>Date</th>
<th>Buttons</th>
<th nzAlign="center" class="fit">
<app-add-btn (click)="addRegistrationRow()" label="{{'common.add' | translate}}"></app-add-btn>
</th>
</tr>
</thead>
<tbody formArrayName="registrations">
<tr *ngFor="let reg of registrations.controls; let i = index" [formGroupName]="i">
<td>
<!--FormControl goes here like an input-->
</td>
<td>
<ng-container *ngFor="let date of setDate.controls let ind = index" [formGroupName]="ind">
<!--Not so shure about this part, any input i set here doesn't show in the page is like a blank cellulare-->
</ng-container>
</td>
<td nzAlign="center" class="fit">
<app-delete-btn (click)="removeRegistrationRow(i)"></app-delete-btn>
<app-add-btn (click)="addSetDateRow()" label="{{'common.add' | translate}}"></app-add-btn> <!-- this doesn't work -->
</td>
</tr>
</tbody>再次为迟到感到抱歉!
发布于 2022-09-25 07:37:45
您需要向父FormArray提供索引。
getSetDateControlFromRegistrationForm(registrationIndex: number): FormArray {
return (this.registrations.controls[registrationIndex] as FormGroup)
.get('setDate') as FormArray;
}怀疑通过提供索引作为参数,不能将该方法作为getter应用。
更新
基于附加的HTML模板,所以您需要的是:
formArrayName="setDate"的包装器setDate FormArray控件。<ng-container formArrayName="setDate">
<ng-container
*ngFor="
let date of getSetDateControlFromRegistrationForm(i).controls;
let ind = index
"
>
<!-- Form Group in each setDate Form Array -->
<div [formGroupName]="ind">
<input formControlName="i" />
<input formControlName="date" />
</div>
</ng-container>
</ng-container>https://stackoverflow.com/questions/73842587
复制相似问题