3

Basically I would like to perform Object.assign to get copy of my data, without anything that AngularJS attached in the past, it attaches now, or may attach in future versions.

Sure I can delete such property as $$hashKey after assignment but this approach is totally fragile, on the other hand I could manually construct the object with fields I want but this on the other hand is tiresome (and also fragile if I change definition of my source object).

Is there something solid in between?

3
  • You don't explain what is this 'data' and how comes that AngularJS attaches 'something' to it. Should it be mentioned that this is the most important part in the question? Commented Jul 15, 2016 at 18:55
  • @estus, data is any object for example you might have, but once you use it in context of Angular, say you use it as model, you may end with your data + Angular data attached to it. So later when you copy it, you copy Angular attached fields as well -- like $$hashKey. Commented Jul 15, 2016 at 20:18
  • Possible duplicate of stackoverflow.com/questions/32344495/remove-hashkey-from-array Commented Jul 15, 2016 at 22:05

2 Answers 2

4

There are no other properties as $$hashKey, it is one of a kind.

All of Angular object helpers are aware of this property and remove it at the end of the operation. angular.extend is a direct Angular counterpart of Object.assign and should be used instead.

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

2 Comments

Thank you! As for "there are no other properties" you are describing current state, there is no guarantee in future there would be no other property or more of them.
@greenoldman $$hashKey is there from the framework's childhood, I guess it is still there and is enumerable for compatibility reasons, the team may be qualified enough to not do this again. Any way, stick to angular.extend({}, ...) to take internals into consideration.
1

angular.copy seems to be helpful in this case

1 Comment

Thank you, there is a difference though, angular.copy is deep copy, while Object.assign is a shallow copy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.