0

I am getting ajax request data and assigning to one scope variable $scope.customerEvents = data; then I am using another variable to modify the data

    var datanew=data;
    datanew.unshift({'customer_events_id':   'ID','date':'Date','event':'Event','eventsubtype':'Event Subtype','eventtype':'Event Type','time':'Time','user_id':'User ID','user_mrn':'User MRN','user_name':'User Name','user_role':'User Role'});
    $scope.downloadcsv=datanew;

But customerEvents is getting updated.

1
  • If my answer solved your problem, can you please accept it? Commented Aug 9, 2016 at 20:11

3 Answers 3

3

you should use:

$scope.customerEvents = angular.copy(data);

Creates a deep copy of source, which should be an object or an array.

That's the documentation.

Also you can look at this question: Javascript equivalent of assign by reference?

Sign up to request clarification or add additional context in comments.

Comments

0

Sure, customerEvents and datanew are probably both referencing the same object (referenced by 'data'). Clone the array when you copy it to datanew and you'll be modifying only datanew and eventually $scope.downloadcsv and not customerEvents.

change var datanew=data; to var datanew=data.slice();

Comments

0

you have to do a deep copy of your object unless it will be referenced by the first variable. you can use:

  • angular.copy(data)

  • angular.merge(dst, src) : documentation.

  • or you can use jquery.clone()

Also you can refer to this post : Deep copying objects in angular?

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.