I am trying to generate dynamic form using the array. In that array I am providing the FieldLabel & DataModel. Here I want to use DataModel as the object reference. I want to update the referenced model when user update the value in Input Field. I have searched a lot but not able to find the solution yet.
Is there any other way to achieve what I want?
I have tried Iterating the model and used the DataModel attribute in [(ngModel)] but it is taking that as a STRING (which is valid) but I don't want this. I want to reference the main object instead of taking that as a string.
obj : any = {
FirstName:"Taha",
MiddleName:"Faheem",
LastName:"Hussain",
Address:
{
CurrentAddress:"USA",
PermanentAddress:"Pakistan"
}
}
fields: any[] = [
{
"FieldLabel": "First Name",
"DataModel": "obj.FirstName"
},
{
"FieldLabel": "Middle Name",
"DataModel":"obj.MiddleName"
},
{
"FieldLabel": "Current Address",
"DataModel":"obj.Address.CurrentAddress"
}
];
<div class="container" fxLayout="column" fxLayoutGap="10px">
<div *ngFor="let field of fields">
<input placeholder="{{field.FieldLabel | uppercase}}" [(ngModel)]="field.DataModel">
</div>
<div>
<input placeholder="Last Name" [(ngModel)]="obj.LastName">
</div>
<br> {{obj|json}}
</div>
I want to update the obj with the value user enter in ngFor input fields.
I am working to generated the complete dynamic form along with the formula on each fields using this approach. Is there any better approach to get the desired result?