我正在尝试使用 ngx-formly 来呈现表单。(使用 FormlyFormConfig 对象)表单正在正确呈现,我将模型值作为 json 对象获取。
但是有没有可能我可以将值设置为 FormlyFormConfig“defaultValue”字段,而不是将表单字段值设置为模型值?
如果没有,是否可以为模型设置其他属性。
这是因为我的表单与来自后端的字段 id 相关联,并且希望将值设置为相同的 formfieldconfig,以便后端知道这些值属于哪个字段。
我正在尝试使用 ngx-formly 来呈现表单。(使用 FormlyFormConfig 对象)表单正在正确呈现,我将模型值作为 json 对象获取。
但是有没有可能我可以将值设置为 FormlyFormConfig“defaultValue”字段,而不是将表单字段值设置为模型值?
如果没有,是否可以为模型设置其他属性。
这是因为我的表单与来自后端的字段 id 相关联,并且希望将值设置为相同的 formfieldconfig,以便后端知道这些值属于哪个字段。
正如我正确理解您的问题一样,您可以向该字段添加默认值。例如:
test.component.ts:
public form = new FormGroup({});
public model = {};
public options: FormlyFormOptions = {};
public fields: FormlyFieldConfig[];
this.fields = [{
key: 'name',
type: 'input',
defaultValue: name, // you can add default value
templateOptions: {
label: 'Name',
placeholder: 'Name',
},
}];
test.component.html:
<formly-form
[form]="form"
[fields]="fields"
[model]="model"
[options]="options"
></formly-form>