【发布时间】:2020-07-26 08:18:48
【问题描述】:
我想将${field.formControl.value} 的值分配给我的JSON 对象内的变量{{fieldName}}。这是我的 JSON 对象:
{
"sectionA": {
"error": {
"allZeros": "All Zero's not allowed.",
"required": "{{fieldName}} is required.",
"patternErrors": {
"userAccountFirstName": "{{fieldName}} Invalid Name. Please enter [A-Z a-z]",
"userAccountLastName": "{{fieldName}} Invalid Lastname. Please enter [A-Z a-z]",
}
}
}
}
我正在使用ngx-formly 来构建动态表单,我想显示基于动态语言的错误。如何为 JSON 中的 {{fieldName}} 赋值?
这是我制作动态形式对象的方法。
控制来自forEach循环fields.forEach(control,index)
myObject =
{
'key': control.name,
'type': 'input',
'templateOptions':
{
'label': control.label,
'pattern': control.validation.pattern,
'required': true
}
'validation':
{
'required': (error, field: FormlyFieldConfig) => `${field.templateOptions.label} ${myJSONresponse.error.required}`,
'pattern': (error, field: FormlyFieldConfig) => `"${field.formControl.value}" ${myJSONresponse.error.patternErrors[control.name]}`,
}
}
【问题讨论】:
-
你是不是漏掉了引号? [检查两行] 'required': (error, field: FormlyFieldConfig) =>
${field.templateOptions.label} ${myJSONresponse.error.required}, 'pattern': (error, field: FormlyFieldConfig) =>"${field.formControl.value}" ${myJSONresponse.error.patternErrors[control.name]}, -
不,是正确的。
标签: javascript json angular typescript ngx-formly