m1crdy m1crdy - 1 year ago 65
AngularJS Question

Angular refresh scope from service with initial data

I have a service returning static JSON data saving to a

variable like this:

$scope.workout = Workouts.get(id);

I´m manipulation this
. When I try to set
to the initial data from the Service, this does not work.

$scope.workout = Workouts.get(id);

has been changed. I thought that another call of the service would refresh the data? Am I wrong? How to get the initial data saved in the service?

Answer Source

The object is a reference type. So why you return it from the service, it returns it's reference. It means that you have 2 reference to the same object (from the service and in the scope). So if you change the object from one reference, it will be changed for another reference too.
You need to copy your object. Simply you can do it by

angular.copy(Workouts.get(id), $scope.workout);

And work with your $scope.workout in your controller.