0

Is there a way to unbind the reference when we are assigning values from scope properties?

For example I have $scope.X and $scope.Y. At some point in my function I want to assign the value of $scope.Y at that time to $scope.X:

$scope.X = $scope.Y

However, whenever scope.Y changes, looks like $scope.X changes too. Is there a way to avoid the chain? I just want to assign the value of $scope.Y once.

I tried using angular copy but it didn't work:

$scope.X = angular.copy([$scope.Y])[0];

Thanks

blenzcoffee
  • 851
  • 1
  • 11
  • 35
  • What about `$scope.X = angular.copy($scope.Y)`? Not sure why you were using arrays – Phil Mar 04 '15 at 00:10
  • 1
    $scope.X = $scope.Y should work... maybe these abstract versions you posted are not the best examples. Can you please create a plunk or fiddle of your problem? Workings perfectly for me... http://plnkr.co/edit/9pdnJHcPMwIeFHIemBBO?p=preview – yangli-io Mar 04 '15 at 00:17

1 Answers1

0

angular.copy should produce a clone of the object. There are a few other methods that may be perform better, see What is the most efficient way to deep clone an object in JavaScript? . I already use lodash for other stuff in my site so I use the _.cloneDeep method.

Community
  • 1
  • 1