0

How can we use two different instances of scope objects

For example :

$scope.seriesdata = [
        [65, 59, 80, 81, 56, 55, 40],
        [28, 48, 40, 19, 86, 27, 90]
      ];
$scope.currentseries =  $scope.seriesdata;

Now if I update $scope.currentseries , $scope.series got automatically updated.

For Example :

$scope.currentseriesdata.splice(index,1);

This updates both $scope.currentseriesdata and $scope.seriesdata

Before writing this : I read this

I dont need to work in factory .

I just need to know how to have different instances within controller

2
  • 2
    '$scope.currentseries = angular.copy($scope.seriesdata);' Making a second variable equal to an array just adds a reference, it does not make a copy. Commented May 5, 2015 at 13:58
  • 1
    Use 'angular.copy();' Commented May 5, 2015 at 15:17

2 Answers 2

3

Just copy the contents before operating on them:

$scope.currentseries = angular.copy($scope.seriesdata);
Sign up to request clarification or add additional context in comments.

Comments

1

It's because you're assigning to currentseries reference to the array that is already referenced by seriesdata. This means you have two variables that point to one array.

You want to use slice

$scope.currentseries =  $scope.seriesdata.slice();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.