-
Notifications
You must be signed in to change notification settings - Fork 27.5k
angular.copy() doesn't strip $$hashKey #1875
Comments
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. |
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. |
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
It is true that objects that are equal must have the same hash value, but the reverse is not true. 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). |
Copying the $$hashKey as part of copy/extend operations makes little sense since hashkey is used primarily as an object id, especially in the context of the ngRepeat directive. This change maintains the existing $$hashKey of an object that is being copied into (likewise for extend). It is not uncommon to take an item in a collection, copy it, and then append it to the collection. By copying the $$hashKey, this leads to duplicate object errors with the current ngRepeat. Closes #1875
For better understanding on angular.copy() visit this article |
Just use angular.toJson(obj) to get rid of the $$hashKey's |
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?
The text was updated successfully, but these errors were encountered: