**Problem:**
I have an array of the input field properties like this.
data = [
{
name:"FirstName"
}
{
name: "MobileNo",
min: 10,
max:14
}
]
This is my HTML form code.
<button (click)="addFormFeild()">Add form feilds</button>
<form
[formGroup]="distributionAddForm"
(ngSubmit)="onSubmit(distributionAddForm.value)"
>
<div class="form-group">
<label>Status :</label>
<div class="input-group input-group-alternative mb-3">
<input
class="form-control"
id="address"
type="address"
formControlName="address"
required
/>
</div>
</div>
</form>
This is my component.ts file code.
import { Component } from "@angular/core";
import {
Validators,
FormGroup,
FormArray,
FormBuilder,
FormControl
} from "@angular/forms";
const data = [{ name: "First Name" }, { name: "MobileNo", min: 10, max: 14 }];
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
public distributionAddForm: any;
constructor(private formBuilder: FormBuilder) {
this.distributionAddForm = this.formBuilder.group({
address: new FormControl("", Validators.required)
});
}
addFormFeild() {
data.map((item, index) => {
});
}
onSubmit(data) {
console.log(data);
}
}
what I want to do is when clicking on add Form field button it should loop through data array and it should add a form field to distributionAddForm and it should render a form field in the form. I tried a lot of ways to figure out how to do it correctly but I was unable to find out a way to do it. If someone can help me to find out a solution to this problem it would be grateful to me. Here I am providing a sandbox link if needed sandbox. Thank you very much.