I have a angular reactive form, where I want to add to fields on button click. So far I can add the fields on button click and it works. But there are two problems I do not really understand and do not have a idea.
- Problem is, I only can write one letter and then it does not continue
- Problem is After I close the modal where my form is I want the complete form to reset. I did it on save and for the entered values it works. But not for the added input fields. They always stay.
Could someone look at it and give me a pointer maybe?
this is my ts file where I intialize my form:
ngOnInit() {
this.materialAddForm = this.fb.group({
name: new FormControl(''),
materialCode: new FormControl(''),
materialType: new FormControl(''),
quantity: new FormControl(''),
buyInformation: this.fb.array([])
})
}
saveMaterial() {
this.materialService.addMaterial(this.materialModel).subscribe(console.log) ;
this.materialAddForm.reset()
console.log(this.materialModel)
}
addSellerInput() {
const buyInformation = this.materialAddForm.controls.buyInformation as FormArray;
buyInformation.push(this.fb.group({
seller: new FormControl(''),
price: new FormControl(''),
}));
}
This is my html (only the relevant part):
<div class="form-row mb-4" formArrayName="buyInformation"
*ngFor="let buyInformation of materialAddForm.controls['buyInformation']?.value; let i = index">
<div class="input-group input-group-sm col-12 col-md-6 mb-4" [formGroupName]="i">
<div class="input-group-prepend">
<span class="input-group-text" id="sellerName">Seller Name</span>
</div>
<input type="text" class="form-control" aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-sm"
formControlName="seller"
>
</div>
<div class="input-group input-group-sm col-12 col-md-6 mb-4" [formGroupName]="i">
<div class="input-group-prepend">
<span class="input-group-text" id="materialUnitPrice">Price</span>
</div>
<input type="text" class="form-control" aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-sm"
formControlName="price"
[(ngModel)]="materialModel.price" name="price">
</div>
</div>
</div>