向 angular 添加新对象属性的正确方法是什么formGroup
?
我有这个设置:
ngOnInit() {
this.form = this.fb.group({
// store is the formGroupname
store: this.fb.group({
// branch and code are formControlName
branch: ['asdf', Validators.required],
code: ['asdf', Validators.required],
}),
selector: this.createStock({}),
stock: this.fb.array([
this.createStock({product_id: '1', quantity: 20}),
this.createStock({product_id: '2', quantity: 30}),
this.createStock({product_id: '3', quantity: 40}),
]),
});
}
在 store 属性中,如果单击了复选框,我想添加。在阅读角度文档后,我有这个解决方案正在运行,但在 vscode 上给了我红线。我想知道这是正确的方法吗?
解决方案:
onSubmitForm() {
// adding dynamic formgroup
if(this.form.get('checkBoxStore').value)
this.form.get('store').addControl(
'foo',
this.fb.group({
testingAdd: this.form.get('test').value,
})
);
}
图片: