标签云

微信群

扫码加入我们

WeChat QR Code

This question already has an answer here:What's alternative to angular.copy in Angular11 answersWhat is the most efficient way to deep clone an object in JavaScript?73 answersI am working with Angular 2 with TypeScript. I have User Management component whereI have table of whole users.When any user in table is clicked then forms appeaer with his whole properties to edit. Choosing user occurs event as below: onUserSelected(event) {var selectedId = event.data.id;this.selectedUser = this.users.filter(user => user.id === selectedId)[0]}The problem is when selectedUser is being edited his properties also changes in table and it doesnt look so good. I tried to create copy myself as below but it didn't help - user class clone() {var cloned = new User(this.id, this.login, this.name, this.surname, this.phone);return cloned;}Maybe I am doing something which is not good practice in Angular2?


what is the problem that you are facing

2019年06月19日15分00秒

Try some already raised question on stackoverflow stackoverflow.com/questions/28150967/typescript-cloning-object

2019年06月19日15分00秒

Side note: Object.assign() is for shallow copies (NOT deep copies), which in the case of this question should work.

2019年06月19日15分00秒

You're right, edited.

2019年06月19日15分00秒

For deep copies, there's always the controversial obj = JSON.parse(JSON.stringify(o));

2019年06月19日15分00秒

Spread is syntactic sugar: this.user = { ...currentObject }

2019年06月19日15分00秒

AngularChef does JSON.stringify work well with dates? I had issues in the recent past.

2019年06月19日15分00秒

How can I use it? _.cloneDeep(...) gives me an Cannot find name '_'. error.

2019年06月19日15分00秒

import * as _ from 'lodash';

2019年06月19日15分00秒

import { cloneDeep } from 'lodash';

2019年06月19日15分00秒