I was trying to get the data from a reactive form, the data here is pre-populated in the html like this:-
<form [formGroup]="editForm" (ngSubmit)="saveHandler()">
<div *ngFor = "let dataItem of dataItems">
<input type="text" value="{{dataItem.id}}" formControlName="id" placeholder="id">
<strong>Name: </strong>
<input type="text" value="{{dataItem.employee_name}}" formControlName="employee_name" placeholder="name">
<strong>Salary :</strong>
<input type="text" value="{{dataItem.employee_salary}}" formControlName="employee_salary" placeholder="salary">
<strong>Age :</strong>
<input type="text" value="{{dataItem.employee_age}}" formControlName="employee_age" placeholder="age">
<button type="submit">Submit</button>
</div>
</form>
In this code snippet the dataItem is an JSON array from the components with the following fields, now I wanted to pass the data of this form back to the component, I cannot use the value directly from the component that is out of my scenario. Here is the dataItem:
this.dataItems = [{
"id":"1",
"employee_name":"bappi gillu",
"employee_salary":"2147483647",
"employee_age":"9876443"
}];
For the reactive form this is the code in the component file:
this.editForm = new FormGroup({
id : new FormControl(null),
employee_name : new FormControl(null),
employee_salary : new FormControl(null),
employee_age : new FormControl(null),
});
and here is the ngSubmit's saveHandler() :
public saveHandler( ){
const employee = this.editForm.value ;
console.log(employee );
}
I am getting null If the form has the css class ng-untouched although if the css class is ng-touched the value is coming perfectly. Now what I wanted is to submit the data irrespective of the class. Here also is a link for the stackblitz
Note: The value for the dataItem is coming from an api for this question I had put it this way.