angular - 表单数组推送为数组中的倒数第二项
问题描述
我有以下代码将新项目推送到表单数组的最后:
(<FormArray>this.invoicesForm.get('invoices')).push(this.fBuilder.group({
id: null,
issueDate: [null, Validators.required], // Only fill in and set the required validator for issue date if a custom date has already been chosen
invoiceIssuedStateCode: [0],
canEditIssueDate: [true],
chooseDate: [false],
issued: [false],
paidStatus: [0],
reference: [null],
creditNotes: this.buildCreditNotes([]),
lineItems: this.buildNewLineItems(this.lineItemsRecord.lineItems)
}));
但是我需要将此项目作为数组中的倒数第二个项目推送。我该怎么办?
解决方案
而不是推,你可以拼接它:
它会这样工作:
(<FormArray>this.invoicesForm.get('invoices')).controls
// splicing instead of pushing
.splice(-2, 0, this.fBuilder.group({
// your fb config
}));
注意 -2(索引 -2 等于长度 - 2)和 0。-2 表示插入或删除的位置,0 表示您正在删除 0 个可迭代对象,然后插入您传递的对象(fb.group ())。
简而言之:您的目标是索引 -2(在最后一项之前),您告诉它删除 0 个项目,然后您在该索引处推送一个新项目(您的 formGroup 对象)。这就是拼接的工作原理!
推荐阅读
- python - 初学者问题:如何合并“功能”和“短语”
- javascript - 如何使用 Resonance Audio 的 setPosition 以像素 (x,y) 为单位定义音频源在 javascript 画布元素上的位置?
- powershell - Powershell JAR 可执行文件占用实例
- android - 创建保存下载文件的路径
- php - HTML 表单未向 Laravel 服务器端发送任何数据
- wpf - 将新行添加到 WPF 中的 DataGrid 时未激活 CellEditingTemplate
- angular - 尝试以角度从 localhost 获取数据时出现 404 Not Found 错误
- c# - 使用 MediaPlayer 在 WPF 应用程序中播放声音
- javascript - Nodemailer 无法在 PUG 引擎上启动
- c# - 动态数据类型的方法重载