i am working with Angular table and reactive forms
- I have a form, when the fields are completed, this data goes into a table and the form is reset.
- I am using reactive forms. At the end, the user should send all the data completed at once, all this with only one form with formArray
- Behavior I have: when the user loads a data and sends to the table, the table is updated correctly, but it creates another form again (not the expected behavior)
This is what i have: StackBlitz
makeForm() {
this.myForm = this.fB.group({
// Cabecera
planilla: [null, Validators.required],
fecha: [null, Validators.required],
// Detalle
units: this.fB.array([
this.getUnit()
])
});
}
private getUnit() {
return this.fB.group({
codigo: null,
vencimiento: null,
lote: null,
cantidad: null,
precio_individual: null,
total_precio: null,
});
}
addUnit(value) {
let model = { codigo: value[0].codigo, descripcion: value[0].descripcion, lote: value[0].lote, mes: value[0].mes, anho: value[0].anho, cantidad: value[0].cantidad, precio_individual: value[0].precio_individual, total: value[0].total_precio};
this.test.push(model);
this.test = [...this.test];
console.log(this.test);
const control = <FormArray>this.myForm.controls['units'];
control.push(this.getUnit());
this.myForm.get('units').reset();
}
I know that the problem comes when the user loads a new data, a new control is created, then the same inputs are created, because in my html i have this:
*ngFor="let unit of this.entradaProductoForm.get('units')['controls']; let i = index"
When the user loads a data, it creates a new control then it creates another form like it (I don't want this). How could I get this behavior but keep the user's data in the formArray?