I'm trying to get the values inside the inputs of this form. There is a part where the inputs are generated every time that the user presses the button. However, after reading the documentation, I have not clear how should I do the .html in order to get the data properly.
component.html
<form [formGroup]="formulario">
<label>
Capacidad
<input type="text" formControlName="capacidad">
</label>
<label>
Max. Casas
<input type="text" formControlName="maxcasas">
</label>
<div formArrayName="pesos">
Peso <button (click)="addPeso()">Añadir peso</button>
<div *ngFor="let peso of pesos.controls; let i=index">
<label>
Casa:
<input type="text" [formGroupName]="i">
</label>
<label>
Peso:
<input type="text" [formGroupName]="i">
</label>
</div>
</div>
</form>
component.ts (where fb is in constructor: constructor(private fb: FormBuilder)
public formulario = this.fb.group({
capacidad: ['24300'],
maxcasas: ['3'],
pesos: this.fb.array([
this.fb.group({
peso: this.fb.control(''),
nombre: this.fb.control(''),
})
])
});
get pesos(){
return this.formulario.get('pesos') as FormArray;
}
addPeso(){
console.log(this.formulario.get('pesos'));
this.pesos.push(this.fb.group([]));
}