问题描述: 在使用Angular Formly的fieldGroup时,对于键为整数的字段无法正常工作。
解决方法:
修改模型数据结构: 将键为整数的字段改为字符串类型的键。例如,将键为整数的字段改为字符串类型的键"1"。
修改formly配置: 在fieldGroup中使用字符串类型的键来引用字段。例如,使用"1"来引用键为"1"的字段。
以下是一个示例代码,演示了如何解决这个问题:
import { Component } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFormOptions, FormlyFieldConfig } from '@ngx-formly/core';
@Component({
selector: 'app-root',
template: `
`,
})
export class AppComponent {
form = new FormGroup({});
model = {};
options: FormlyFormOptions = {};
fields: FormlyFieldConfig[] = [
{
key: 'fieldGroup',
type: 'formly-group',
fieldGroup: [
{
key: '1', // 键为整数,修改为字符串类型的键'1'
type: 'input',
templateOptions: {
label: 'Field 1',
required: true,
},
},
{
key: '2',
type: 'input',
templateOptions: {
label: 'Field 2',
required: true,
},
},
],
},
];
onSubmit() {
if (this.form.valid) {
console.log(this.model);
}
}
}
通过将键为整数的字段改为字符串类型的键,可以解决在使用Angular Formly的fieldGroup时,对于键为整数的字段无法正常工作的问题。
下一篇:按国家查找最大值和最小值