angular - Is it possible to add property dynamically to groupform
问题描述
Is it possible to add property dynamically to groupform? I need a property name to have an index as part of the name
createForm() {
for (let index = 0; index < 10; index++) {
this.groupForm = this.fb.group({
prixName+index: ['', [Validators.maxLength(4)]],
});
}
}
解决方案
为此使用 addControl 函数
this.testForm.addControl('new', new FormControl('', Validators.required));
在 Angular 中动态查看这个答案addControl to FormGroup和这个链接https://angular.io/api/forms/FormGroup#addcontrol
createForm() {
this.groupForm = this.fb.group({});
for (let index = 0; index < 10; index++) {
this.groupForm.addControl(prixName+index, new FormControl(null, Validators.maxLength(4)));
}
}
如果您需要表单控件数组,请使用 FormArray 而不是索引或增量计数器。在这种情况下,这也将对您有所帮助 https://angular.io/api/forms/FormArray
推荐阅读
- swift - 如何快速更新表格视图单元格约束
- c - rep movsd 在这个二元炸弹阶段的背景下做了什么?
- c# - 在 DataSet 中获取更改并将其保存到数据库的最佳方法
- python - 如何解决错误?“节点”对象在 Google Colab 中没有属性“输出掩码”
- scala - 无法在 Twirl 中呈现 for 循环
- c++ - 排序数组超出大小
- docker - docker-compose build nginx:网络错误(检查互联网连接和防火墙)
- javascript - 有没有办法(在 vanilla javascript 中)防止在一个匹配多个键的数组中重复 rng 生成的对象?
- c - (C) 在数组中找到一个元素并只打印一次它的位置
- spring-boot - 当我们有身份验证系统时,如何为某些链接添加基本身份验证