46

I am working with Angular 2 with TypeScript. I have User Management component where I 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?

2

3 Answers 3

80

Try using

this.user = Object.assign({}, currentObject);

As mentioned by @AngularFrance, this will only work for shallow-copying objects, seek another implementation if there's a need to deep-copy an object.

Sign up to request clarification or add additional context in comments.

8 Comments

Side note: Object.assign() is for shallow copies (NOT deep copies), which in the case of this question should work.
You're right, edited.
For deep copies, there's always the controversial obj = JSON.parse(JSON.stringify(o));
Spread is syntactic sugar: this.user = { ...currentObject }
@AngularChef does JSON.stringify work well with dates? I had issues in the recent past.
|
18

You can use lodash :

https://lodash.com/docs#cloneDeep

lodash is recommended for lot of objects / array manipulations

3 Comments

How can I use it? _.cloneDeep(...) gives me an Cannot find name '_'. error.
import * as _ from 'lodash';
import { cloneDeep } from 'lodash';
7

You could bind your editor form to an empty User object, say editUser, instead of the selectedUser variable (which points to an element of your user collection). In your onUserSelected(event), you'd initialize editUser via cloning the mutable properties of the selected user objects. Upon submitting the edit form ((ngSubmit)="editSubmit()"), you replace the original properties in the selected user object in the user collection.

Something along the lines of:

editUser: User = new User();
selectedId: number;
selectedUser: User;

onUserSelected(event) {
    this.selectedId = event.data.id;
    this.selectedUser = this.users.filter(user => user.id === this.selectedId)[0];
    this.editUser = this.simpleClone(this.selectedUser);
}

editSubmit(event) {
    this.selectedUser = this.simpleClone(this.editUser);
}

simpleClone(obj: any) {
    return Object.assign({}, obj);
}

The simpleClone implementation is not suitable for deep cloning, so if your User objects hold references to other objects, this should be turned into a proper cloning function.

1 Comment

by using an any type simpleClone is bypassing type safety :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.