【发布时间】:2021-01-09 03:15:49
【问题描述】:
我正在尝试使用响应式表单在 Angular 中循环嵌套表单控件。 有一个“taskSchedule”,其中有许多需要循环的“note”API 数据。
this.profileForm = new FormGroup({
userName: new FormControl(this.currentUserData.id),
title: new FormControl(this.taskScheduleData.title),
startDate: new FormControl(this.startDateConvert),
endDate: new FormControl(this.endDateConvert),
// this is where it should loop through the notes nested api data but it does not
this.taskScheduleData.forEach(element => {
this.notesForm = new FormGroup({
notesInfo: new FormControl(this.taskScheduleData.notes[element].notesInfo),
});
});
});
API 数据
{
"id": 10,
"title": "Post test with notes",
"start": "2020-09-14T12:00:00",
"end": "2020-09-14T17:30:00",
"userId": 2,
"notes": [
{
"id": 4,
"notesInfo": "This is third notes",
"dateCreated": "2020-09-14T12:12:00",
"userId": 2,
"taskScheduleId": 10
},
{
"id": 6,
"notesInfo": "this is second notes",
"dateCreated": "2020-09-20T22:43:00",
"userId": 3,
"taskScheduleId": 10
},
{
"id": 7,
"notesInfo": "this is third notes",
"dateCreated": "2020-09-20T22:43:00",
"userId": 3,
"taskScheduleId": 10
}
]
}
有一个错误提示
阴影名称:'元素'(无阴影变量)
显然我编写的 foreach 循环是不正确的。有没有办法循环遍历 3 个嵌套的“notes”API 数据,以便每个 note 元素都有自己的控制形式?
【问题讨论】:
-
这是一个 linter 错误。请显示完整的功能代码。
-
是
this.taskScheduleDatanotes 数组还是实际响应?
标签: angular typescript angular-reactive-forms reactive-forms form-control