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?

greenoldman
  • 16,895
  • 26
  • 119
  • 185
  • 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? – Estus Flask Jul 15 '16 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`. – greenoldman Jul 15 '16 at 20:18
  • Possible duplicate of http://stackoverflow.com/questions/32344495/remove-hashkey-from-array – Estus Flask Jul 15 '16 at 22:05

2 Answers2

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.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • 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 Jul 16 '16 at 06:38
  • @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. – Estus Flask Jul 16 '16 at 13:14
1

angular.copy seems to be helpful in this case

Sergey Mell
  • 7,780
  • 1
  • 26
  • 50