Using Dynamic forms, I need to give users the ability to create forms fields dynamically. There needs to be a 'Add' button and when user clicks on this button, 4 input fields would be created.
I am able to create one field dynamically so far. Below is my code
In the HTML, I am using index to create the formControlName
<div class="row">
<div class="col-xs-12">
<div formArrayName="properties">
<button class="btn btn-default btn-xs" type="button" (click)="onAddProperty()">Add</button>
<div class="form-group" *ngFor="let propertyControl of searchForm.get('properties').controls; let i = index">
<input type="text" class="form-control" [formControlName]="i">
<!-- <input type="text" class="form-control" [formControlName]="'B'"+"i">
<input type="text" class="form-control" [formControlName]="'C'"+"i">
<input type="text" class="form-control" [formControlName]="'D'"+"i"> -->
</div>
</div>
</div>
</div>
and then in the component, pushing the control to the FormArray
searchForm: FormGroup;
onAddProperty() {
const control = new FormControl(null);
(<FormArray>this.searchForm.get('properties')).push(control);
}
Now, I can click 'Add' button any number of times, and for every click one input field will be created.
However, instead of one input field, I need 4 inputs fields to be created for every click. I am not able to make out how to give each of these form fields unique formControlName. Right now, I am using index of the field which would be unique for 1 field, but wouldn't be so for 4 fields.