Angular.copy () not deep copying reference arrays

In my Angular application, I have an array that refers to the coordinates of the polygon. For instance:

[[-1,0], [0,1], [1,0], [0,-1], [-1,0]]

The important thing here is that the first and last points are repeated and actually refer to the same array of length 2 lengths. This is the result of the plugin that I am using. However, arrays are created several times in such a way that the first and last points with the same value are not the same reference.

At some point in my Angular application, I need to create a new polygon with the same coordinates as the original, only turned upside down. My first attempt:

var newCoords = angular.copy(polygon.coordinates);
for (var i = 0; i < newCoords.length; i++) {
  newCoords[i].reverse();
}

However, in cases where the first and last coordinates have the same link, I hit one of two reversible points.

I realized that I was angular.copy()creating a deep copy of what was being transmitted, and I should not experience this problem. It’s clear that this is wrong, so why? Is there a way to make a really deep copy of the coordinate array that eliminates this odd link mapping? At the moment, I managed to get around this, adding even angular.copy(newCoords[i])before reverse().

+4
source share
1 answer

As pointed out in the comments, this is due to a change inside the angular core. This change was released in Angular v1.2.17 . The bug fix was indicated as:

angular.copy: (5c997209, # 7618)

. , . , . , :

var newCoords = angular.copy(polygon.coordinates);
// Ensure unique reference for the first element
newCoords[0] = angular.copy(newCoords[0]);

- angular v.1.2.17 ( v0.9.0), .


, , ( ), , angular v1.4.8. , . , 1.2.17, :

var stackSource = angular.extend([], {push:angular.noop});
var newCoords = angular.copy(polygon.coordinates, null, noopArray);

- stackSource. push, , . : (, v1.2.16 down), 1.4.8 - . .

+1

All Articles