Currently, I am experimenting with building a dynamic reactive form using Angular. While I have successfully implemented the looping functionality, I am facing some challenges in displaying it the way I want.
<form [formGroup]="registerForm" (ngSubmit)="onSubmit()">
<div *ngFor="let dat of formData; let index = index;">
<label for="{{dat.name}}">{{dat.name }}</label>
<input type="text" id="{{dat.id}}" [formControlName]="dat.name" />
</div>
</form>
<button type="button"><button>
.ts
registerForm: FormGroup;
submitted = false;
formData = [{ id: 'firstName', name: 'firstName' },
{ id: 'lastName', name: 'lastName' },
{ id: 'address', name: 'address' },
{ id: 'emailid', name: 'emailid' }
]
constructor(private formBuilder: FormBuilder) {
this.registerForm = this.formBuilder.group({
formData: []
});
}
onSubmit(){
console.log(this.registerForm.value);
}
Moreover, I am looking to add validation to the form as well.
You can check out my code on StackBlitz by following this URL: