You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular中FormControl序列化循环引用问题及保留表单值的解决方案咨询

Angular中FormControl序列化循环引用问题及保留表单值的解决方案咨询

嘿,我最近在开发Angular项目时碰到了一个棘手的问题,想跟大家请教下可行的解决方案:

我定义了一个包含以下属性的TypeScript类:

date_from_control       : FormControl = new FormControl();
date_to_control         : FormControl = new FormControl();
date_from               : Date;
date_to                 : Date;

这个类和mat-datepicker表单字段配合使用,对应的模板代码如下:

<mat-form-field  appearance="outline">
  <input matInput
    [matDatepicker]='picker_from_i'
    [formControl]="item.date_from_control"
  >
  <mat-datepicker-toggle matSuffix [for]="picker_from_i"></mat-datepicker-toggle>
  <mat-datepicker #picker_from_i></mat-datepicker>
</mat-form-field>

但当我尝试把这个对象通过API发送给后端时,出现了序列化错误:

Converting circular structure to JSON --> starting at object with constructor 'SafeSubscriber' | property '_finalizers' -> object with con

我试过在调用服务前把这两个FormControl属性设为null,这样就能正常提交了,但这么做会丢失表单里已填写的值——如果服务器返回错误,表单就会变成空白状态,用户再次点击保存时可能会提交空日期,这显然不是我想要的结果。

有没有什么办法既能解决序列化的循环引用问题,又能保留表单里的已填值呢?

备注:内容来源于stack exchange,提问作者EviSvil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 09:48:01