Reputation: 81
Trying to achieve: Dynamic Forms as per user selected locale JSON using ngx-formly/material. How to map fields with my array of objects "this.fields2"?
In my component.ts I have:
model: any = {};
options: FormlyFormOptions = {};
fields: FormlyFeildCongif[];
Fetching the JSON of the selected Locale:
ngOnInit():void
{
this.langService.getSelectedLocaleData(this.cuurentLang).subscribe(
(res) =>
{
this.myDynamicForm = res.yBank.fields;
this.dynamicFormCreate(this.myDynamicForm);
});
}
public dynamicFormCreate(arr:any)
{
for(i=0; i< arrr.lenght; i++)
{
//here I am fetching data from json and creating an Object structure which is accepted by formly feilds
//the problem is how do I map this.fields2 array of objects with the formly feilds
this.fields2.push(
{
key: arr[i].type,
type: arr[i].type,
templateOptions:
{
label: arr[i].label,
placeHolder: arr[i].placeHolder,
description: arrp[i].description,
required: true
}
});
}
}
my component.html
<form [formGroup]="myForm">
<formly-form
[model]="model
[fields]="fields"
[options]="options"
[form]="myForm">
</formly-form>
</form>
Upvotes: 4
Views: 3131
Reputation: 29
If I understand your question correctly then you have a couple of ways to resolve this:
Assign fields2 to fields
Assuming your, this.fields2 is also a FormlyFieldConfig[], Just assign it to the formly fields in the ngOnInit
ngOnInit() {
this.langService.getSelectedLocaleData(this.cuurentLang).subscribe(...);
this.fields = this.fields2;
Use this.fields inside the dynamicFormCreate method
Or you can directly update this.fields in your subscribe (instead of using fields2).
public dynamicFormCreate(arr:any)
{
for(i=0; i< arrr.lenght; i++)
{
//here I am fetching data from json and creating an Object structure which is accepted by formly feilds
//the problem is how do I map this.fields2 array of objects with the formly feilds
this.fields.push(); // USING fields instead of fields2
...
Upvotes: 2