我试图用给定的键名和值来构建角形形式,这些键总是相同的.我已经设法建立一个表单,但输出格式不是我需要的.
keysToChange = ['key1', 'key2', 'key3', 'key4']
export interface PatchForm {
[x: string]: FormControl<string | null>
date: FormControl<string | null>
}
patchForm = this.fb.group({
keysToChange: this.fb.array<FormGroup<PatchForm>>([]),
})
initializeForm() {
this.keysToChange.forEach((key) => {
const formGroup = this.fb.nonNullable.group({
[key]: new FormControl(''),
date: new FormControl(''),
})
this.patchForm.controls.keysToChange.push(formGroup)
})
}
I get this output:
{
"keysToChange": [
{
"key1": "",
"date": ""
},
{
"key2": "",
"date": ""
},
{
"key3": "",
"date": ""
},
{
"key4": "",
"date": ""
}
]
}
但我需要更多这样的东西
export interface PatchForm {
id: FormControl<string | null>
date: FormControl<string | null>
}
then output like this:
{
"key1": {
"id": "string",
"date": "2024-04-03T11:23:05.499Z"
},
"key2": {
"id": "string",
"date": "2024-04-03T11:23:05.499Z"
},
"key3": {
"id": "string",
"date": "2024-04-03T11:23:05.499Z"
},
"key4": {
"id": "string",
"date": "2024-04-03T11:23:05.499Z"
}
}
我遇到的问题主要是创建类型和初始化表单,因为我的eslint不允许不安全地使用类型'any',我不太明白如何初始化每个FormGroup的键.