javascript - 修改 Javascript 对象副本会使原始对象在 Angular 5 Reactive Forms 中发生变化
问题描述
我正在尝试创建我的反应式表单值对象的副本(使用扩展运算符或 Object.assign)并在将其发送到我的服务器之前对其应用一些更改。
这是构建我的表单的代码,它在我的模板中按预期工作:
newTask: FormGroup
this.newTask = this.formBuilder.group({
partner: [null, [Validators.required]],
title: [null, [Validators.required, Validators.minLength(5), Validators.maxLength(50)]],
description: [null, [Validators.minLength(10), Validators.maxLength(120)]],
responsible: [null, [Validators.required]],
type: [null, [Validators.required]],
priority: [null, [Validators.required]],
status: [null, [Validators.required]],
history: this.formBuilder.array([])
})
下面的代码说明了如何在历史数组中添加和删除元素:
createAction (action?) {
return this.formBuilder.group({
title: [action ? action.title : undefined, [Validators.required, Validators.minLength(5), Validators.maxLength(50)]],
description: [action ? action.description : undefined, [Validators.required, Validators.minLength(10), Validators.maxLength(200)]],
resolveAt: [action ? action.resolveAt : undefined],
result: [action ? action.response : undefined, [Validators.minLength(10), Validators.maxLength(200)]],
resolvedAt: [action ? action.resolvedAt : undefined],
status: [action ? action.status : undefined, [Validators.required]],
relatedPerson: [action ? action.relatedPerson : undefined]
})
}
addAction (action?): void {
const history = this.newTask.get('history') as FormArray
history.push(this.createAction(action || initialActionState))
}
removeAction (i: number): void {
const history = this.newTask.get('history') as FormArray
history.removeAt(i)
}
在用户正确输入所有需要的数据后,当我提交表单时,这是我必须执行的修改:
let cleanVar = {...this.newTask.value}
// I could also use let cleanVar = Object.assing({}, this.newTask.value), the result is the same
cleanVar.history.forEach(item => {
item.status = item.status.string
if (item.relatedPerson) item.relatedPerson = item.relatedPerson._id
})
console.log(this.newTask.value)
如您所见,我正在尝试修改原始对象的副本,但是当我记录 this.newTask.value 时,更改也会传播到它。
我以前从未在对象副本方面遇到过这样的问题,所以我试图找出为什么会发生这种情况。也许它与我无法检测到的 Angular Reactive Forms 相关。
有人知道为什么会这样吗?
谢谢!
更新
这是我的 newTask.value 变量中的内容:
解决方案
正如 marekful 和 Rikin 建议的那样,使用 lodash cloneDeep 是要走的路:
import { cloneDeep } from 'lodash'
let clonedVar = cloneDeep (originalVar)
// do whatever you need with clonedVar and originalVar isn't touched
感谢大家!
推荐阅读
- html - 使用 ngfor 数组数据在 Angular 中动态更改样式
- java - 如何将多个字符串格式化为数组格式
- python - 自定义 Gtk.CellRenderer,在 True、None(显示为不一致)和 False 之间循环切换,python GTK 3
- angular - 为什么Highlight.js在重定向后不起作用?
- python - Django 中的外键。如何正确创建模型
- ios - 使用 switch 语句创建泛型函数,该语句根据参数中传递的类型执行不同的代码
- yolo - ImportError:没有名为“yolo_utils”的模块:Analytics Vidhya
- sql - 如何在 Hive 中提取小数的一部分
- html - CSS Button 效果在浏览器中不起作用
- c++ - 链接器输入文件未使用,因为链接未完成,我无法获取 .o