如何将数组项推入表单组的数组属性

发布于 2025-02-09 06:20:18 字数 2001 浏览 3 评论 0原文

我的表单组定义为:

this.scheduleParentModel = this.fb.group({
    startDate: ['', dateValidator()],
    endDate: ['', dateValidator()],
    frequency: [1, Validators.required],
    repeatTime: ['', Validators.required],
    frequencyInterval: this.fb.array([],),

}, { validator: this.dateLessThan('startDate', 'endDate') });

API调用后,我想将API的结果绑定到表格组scheduleparentModel。 API还将fusity Interval作为逗号分隔字符串返回。

我已将API结果绑定为:

this.scheduleParentModel.patchValue({
    startDate: this.datepipe.transform(new Date(schedules.startdate), 'mediumDate'),
    endDate: this.datepipe.transform(new Date(schedules.enddate), 'mediumDate'),
    repeatTime: schedules.repeattime,
    frequency: schedules.frequency
});

为了绑定到频率间隔,我做到了:

let frequencyInterval = [...schedules.frequencyinterval.split(',').map(item => item.trim())];
const fInt = this.scheduleParentModel.controls.frequencyInterval as FormArray;
fInt.push(this.fb.group(frequencyInterval));

它变为:

{
    "startDate": "Jun 24, 2022",
    "endDate": "Jun 30, 2022",
    "frequency": 8,
    "repeatTime": "18:00",
    "frequencyInterval": [{
        "0": "5",
        "1": "4",
        "2": "3",
        "3": "2",
        "4": "1"
    }]
}

但是,我希望输出为:

{
    "startDate": "Jun 24, 2022",
    "endDate": "Jun 30, 2022",
    "frequency": 8,
    "repeatTime": "18:00",
    "frequencyInterval": ["5","4","3","2","1"]
}

计划对象:

{
    "enddate": "2022-06-30T00:00:00",
    "frequency": 8,
    "frequencyinterval": "5, 4, 3, 2, 1",
    "lastUpdateOn": "2022-06-18T09:41:26",
    "message": "ffdsdewe",
    "remindafter": false,
    "remindtime": 0,
    "repeattime": "18:00",
    "scheduleid": 10,
    "startdate": "2022-06-24T00:00:00",
    "starttime": "",
    "timezone": null,
    "title": "reset donerecurring"
}

如何将数组项目推到形式组的属性。

I have a form group defined as:

this.scheduleParentModel = this.fb.group({
    startDate: ['', dateValidator()],
    endDate: ['', dateValidator()],
    frequency: [1, Validators.required],
    repeatTime: ['', Validators.required],
    frequencyInterval: this.fb.array([],),

}, { validator: this.dateLessThan('startDate', 'endDate') });

After the API call, I wanted to bind the result from API into the form group scheduleParentModel. API also returns the frequencyinterval as a comma-separated string.

I have bind the api result as:

this.scheduleParentModel.patchValue({
    startDate: this.datepipe.transform(new Date(schedules.startdate), 'mediumDate'),
    endDate: this.datepipe.transform(new Date(schedules.enddate), 'mediumDate'),
    repeatTime: schedules.repeattime,
    frequency: schedules.frequency
});

for binding to the frequency interval, I did, as:

let frequencyInterval = [...schedules.frequencyinterval.split(',').map(item => item.trim())];
const fInt = this.scheduleParentModel.controls.frequencyInterval as FormArray;
fInt.push(this.fb.group(frequencyInterval));

it becomes as:

{
    "startDate": "Jun 24, 2022",
    "endDate": "Jun 30, 2022",
    "frequency": 8,
    "repeatTime": "18:00",
    "frequencyInterval": [{
        "0": "5",
        "1": "4",
        "2": "3",
        "3": "2",
        "4": "1"
    }]
}

however, i wanted the output as:

{
    "startDate": "Jun 24, 2022",
    "endDate": "Jun 30, 2022",
    "frequency": 8,
    "repeatTime": "18:00",
    "frequencyInterval": ["5","4","3","2","1"]
}

Schedule objects:

{
    "enddate": "2022-06-30T00:00:00",
    "frequency": 8,
    "frequencyinterval": "5, 4, 3, 2, 1",
    "lastUpdateOn": "2022-06-18T09:41:26",
    "message": "ffdsdewe",
    "remindafter": false,
    "remindtime": 0,
    "repeattime": "18:00",
    "scheduleid": 10,
    "startdate": "2022-06-24T00:00:00",
    "starttime": "",
    "timezone": null,
    "title": "reset donerecurring"
}

How to push the array items to the properties of form group.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2025-02-16 06:20:18

将其用于绑定到频率间隔

const fInt = this.scheduleParentModel.controls
  .frequencyInterval as FormArray;
let frequencyInterval = schedules.frequencyinterval
  .split(',')
  .forEach((item) => fInt.push(this.fb.control(item.trim())));  

您需要使用控制而不是group

use this for binding to the frequency interval.

const fInt = this.scheduleParentModel.controls
  .frequencyInterval as FormArray;
let frequencyInterval = schedules.frequencyinterval
  .split(',')
  .forEach((item) => fInt.push(this.fb.control(item.trim())));  

you need to use control instead of group

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文