具有反应形式的 Angular patchValue json 数据
我正在编辑页面,并尝试使用从 API 获得的数据设置表单默认值。
我的问题是我的数组中有两个项目,但它创建了另一个空的 formArray;
我做错了什么?
这是我的 stackblitz
this.filterForm = this.fb.group({
categories: this.fb.array([this.initCat()]),
});
const control = <FormArray>this.filterForm.get('categories');
console.log(control);
this.chosenCarPartCategories.carPartCategories.forEach((x) => {
control.push(
this.patchValues(x.name, x.carPartSubCategoryId, x.quantity, x.comment)
);
});
patchValues(name, carPartSubCategoryId, quantity, comment) {
return this.fb.group({
carPartCategory: [name],
carPartSubCategory: [carPartSubCategoryId],
quantity: [quantity],
comment: [comment],
});
}
initCat() {
return this.fb.group({
carPartCategory: ['', Validators.required],
carPartSubCategory: ['', Validators.required],
quantity: ['', Validators.required],
comment: [''],
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难理解你想在这里做什么,仅供参考。但您描述的问题来自
this.initCat()
。将其删除以删除空的额外表格。
Very difficult to understand what you're trying to do here, fyi. But your described problem comes from
this.initCat()
.Remove it to remove the empty extra form.