Reputation: 1079
I am using angular formbuilder to create a nested form. How do I set nested form quantity
field value for each form group within updateValue()
function in following code.
ngOnInit() {
this.fastPostingForm = this.fb.group({
Charges: this.fb.array([
this.initCharge()
])
});
}
initCharge(){
return this.fb.group({
room: ['', Validators.required],
amount: ['', Validators.required],
quantity: ['', Validators.required],
});
}
UpdateValue(i) {
this.fastPostingForm.controls.Charges[i].controls['quantity'].setValue(2); // This is not working
}
Upvotes: 1
Views: 1122
Reputation: 214295
It should be:
(this.fastPostingForm.controls.Charges as FormArray).controls[i].controls['quantity']
.setValue(2)
or
this.fastPostingForm.get(['Charges', i, 'quantity']).setValue(2);
or
this.fastPostingForm.get(`Charges.${i}.quantity`).setValue(2);
^^^
backtick
or you can even use patchValue
method on your form:
this.fastPostingForm.patchValue({
Charges: [...Array(i), { quantity: '2' }]
});
Upvotes: 1