How can i prevent binding in this case:
$scope.data = [{i:0, a:0}, {i:0, a:0}, {i:0, a:0}];
$scope.twoofthedata = [$scope.data, $scope.data];
so that $scope.twoofthedata[0] is indipendent from $scope.twoofthedata[1]
How can i prevent binding in this case:
$scope.data = [{i:0, a:0}, {i:0, a:0}, {i:0, a:0}];
$scope.twoofthedata = [$scope.data, $scope.data];
so that $scope.twoofthedata[0] is indipendent from $scope.twoofthedata[1]
In JavaScript, objects are manipulated by reference. This is not an Angular binding issue.
However Angular enables you to bypass this language constraint via the angular.copy method, which creates a deep copy of a given object.
$scope.data = [{i:0, a:0}, {i:0, a:0}, {i:0, a:0}];
// Manually call `angular.copy` on each member
$scope.twoofthedata = [
angular.copy($scope.data),
angular.copy($scope.data)
];
// Cleaner version (using `Array.prototype.map`)
$scope.twoofthedata = [$scope.data, $scope.data].map(angular.copy);
However you should be warned that a deep copy could be very expensive, and therefore only be used when truly necessary.
FYI: if you encounter a similar issue but not using Angular, you could use Lodash which also has a deep clone method.
angular.copy($scope.data) > $.extend(true, {}, $scope.data); now is not showing RangeError: Maximum call stack size exceeded$watch?$.extend?