Angular 9 : Used formarray without form tag in html content but now need to add validations and added form tag to the html component, now code does not work!
Used a formarray to dynamically generate a row of controls(textbox and textarea) on button click;The html component did not have form tag and this was achieved by first creating a formarray:
requirement=new FormArray([]);
and within the html component added the following:
<tr let req of requirement.controls let i=index>
<td>
<input type="number" name="Req#" [(NgModel)]="reqno"/>
</td>
<td>
<textarea type="text" name="Req" [(NgModel)]="req"></textarea>
</td>
:
:
:
</tr>
and on button click applied the following code:
this.requirement.push(new FormControl(''));
This helps in generating dynamic controls but with this no validations can be performed and so added the form tag to the html component and add formControlName instead of NgModel and instead of the above typescript code: declare the formarray as follows:
public requirement:FormArray;
and initialize the same on ngOnInit():
this.requirement=new FormArray([
new FormGroup({
reqno:new FormControl('');
req:new FormControl('');
})
]);
after changing this the Ngif and NgFor does not work.Could anyone help out.