【发布时间】:2023-04-11 03:36:01
【问题描述】:
我正在构建如下图所示的反应形式-
如图所示,每个文件(代码中称为附件)中都有多个议程。议程可以添加、更新和删除
buildForms() {
this.agendaForms = this.formBuilder.group({
attachements: this.formBuilder.array([
])
});
从 web 服务获取表单数据并以这种方式创建表单模型-
data => {
this.submission = data;
// New blank agenda for all attachements
if (
!isNullOrUndefined(this.submission) &&
!isNullOrUndefined(this.submission.attachments)
) {
this.submission.attachments.forEach(attachment => {
const agenda = new NLAgenda();
agenda.dwgNo = attachment.filename;
agenda.dWGNo = attachment.filename;
this.submission.agendas.push(agenda);
if (!isNullOrUndefined(attachment)) {
attachment.agendas = this.getAgendasForAttachment(attachment);
if (!isNullOrUndefined(attachment.agendas)) {
this.attachmentFormArray = this.agendaForms.controls
.attachements as FormArray;
this.attachmentFormArray.push(
this.createAttachmentAgendasControl(attachment.agendas)
);
}
}
});
}
}
模板看起来像这样
<form [formGroup]="agendaForms">
<div formArrayName="attachements">
<div *ngFor="let attachmentFormGroup of attachmentFormArray.controls;
let attachmentId = index">
<!-- Attachment header-->
<div>
<!-- File Name-->
<div>
{{ submission.attachments[attachmentId].filename }}
</div>
<!-- Action Buttons-->
<div>
<input type="button" value="Link To"/>
</div>
</div>
<!-- Agendas -->
<div formGroupName="{{ attachmentId }}">
<div formArrayName="agendas">
<div *ngFor="let agendaFormGroup of attachmentFormGroup.controls.agendas.controls;
let agendaId = index">
<div formGroupName="{{ agendaId }}" >
<mat-form-field>
<input
type="text"
id="project"
placeholder="Sheet No."
formControlName="sheetNumber"
matInput
/>
<!-- <mat-error>{{ getErrorMessage(f.project) }}</mat-error> -->
</mat-form-field>
<mat-form-field>
<input
type="text"
id="project"
placeholder="Title"
formControlName="title"
matInput
/>
</mat-form-field>
<mat-form-field>
<input
type="text"
id="project"
placeholder="Description"
formControlName="description"
matInput
/>
</mat-form-field>
<div>
<a
matTooltip="Add Agenda"
aria-label="Add Agenda"
(click)="
createOrUpdateAgenda(
submission.attachments[attachmentId].agendas[agendaId],
attachmentId,
agendaId
)">
<i class="fa fa-check"></i>
</a>
<a
*ngIf="submission.attachments[attachmentId].agendas[agendaId].created != null && submission.attachments[attachmentId].agendas[agendaId].created != undefined"
matTooltip="Delete Agenda"
aria-label="Delete Agenda"
(click)="deleteAgenda(submission.attachments[attachmentId].agendas[agendaId])">
<i class="fa fa-remove"></i>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
问题: 每当添加或更新议程时,我都会从服务器重新获取提交对象数据以显示文件和议程的更新状态,提交对象更改并在控制台中出现错误,例如“无法读取未定义的属性'议程'”,因为在旧对象的新提交对象位置因新增或删除而改变
我相信我只需要使用一个数组(FormArray)而不是两个数组(Form 数组和提交对象)来构建模板,否则如果一个更改直到其他更改控制台将抛出错误。但是如何只使用FormArray,我在提交对象中有一些数据?有没有办法将提交对象与 FormArray 绑定?
我厌倦了https://github.com/angular/angular/issues/13845,但没有成功,因为我正在使用表单构建并且不知道如何使用表单构建器来完成这个技巧
【问题讨论】:
-
你能在 plunker 或 stackblitz 中给出你的整个代码吗?
-
您的 buildForms() 方法在您的帖子中是否完整?
标签: angular angular-reactive-forms reactive-forms