angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

angular.copy() doesn't strip $$hashKey

Open zivester opened this issue 13 years ago • 5 comments

angular.copy({ name: "DEF", $$hashKey: "089" }); => Object { name= "DEF", $$hashKey= "089" }

I believe it is suppose to strip out $$hashKey, like:

angular.fromJson(angular.toJson({ name: "DEF", $$hashKey: "089" })); => Object { name= "DEF" }

Why aren't the two equivalent?

zivester avatar Jan 24 '13 21:01 zivester

I guess it probably should since the idea is that each object has a unique $$hashKey and now you have to potentially diverging objects with the same key. @IgorMinar & @mhevery - Any thoughts on this?

petebacondarwin avatar Jan 24 '13 22:01 petebacondarwin

With the new change to ng-repeat 1.1.4 that prevents duplicates, this break my code.

My use-case is a simple array, where the user can drag a cell onto another cell to copy it. Now the copy creates a duplicate because the key is copied as well.

For now, I'm just overriding angular.copy with my own version that deletes the hashkey, because there are just too many places in my code where I copy objects.

BTW: extend() also copies the hashkey.

spamdaemon avatar Apr 05 '13 18:04 spamdaemon

I've just commented on that patch, and it occurred to me that the use of hashKey() is kind of confused. First, hashKey() is a misnomer, because

  • the documentation says "...Computes a hash of an 'obj'..."
  • the resulting hash value is used as if two objects with the same hash are equal.

It is true that objects that are equal must have the same hash value, but the reverse is not true. A more appropriate function would be getObjectID or assignObjectID.

Since the "hash" is really used as an object identity value, it makes no sense to copy() or extend() it to another object, ever.

I kind of think that this needs to be addressed, because it will generate subtle problems in any larger application that will be difficult to track down.

I think I have an idea for a patch that might work and should be less intrusive (i.e. not drop all $ properties).

spamdaemon avatar Apr 16 '13 15:04 spamdaemon

For better understanding on angular.copy() visit this article http://sharepoint-2010-world.blogspot.in/2013/10/angularcopy-in-angularjs.html

srikanthbattula avatar Nov 12 '13 08:11 srikanthbattula

Just use angular.toJson(obj) to get rid of the $$hashKey's

bostondevin avatar Aug 11 '17 08:08 bostondevin