0

I have a service returning static JSON data saving to a $scope variable like this:

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

I´m manipulation this $scope.workout with shift() and slice(). When I try to set $scope.workout to the initial data from the Service, this does not work.

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

The $scope.workout 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?

4
  • Are you returning JSON or already converted JSON ? Commented Jul 21, 2016 at 12:43
  • I´m returning an Object. Sorry for that mistake. Commented Jul 21, 2016 at 12:45
  • Please post complete code. Commented Jul 21, 2016 at 12:46
  • 1
    What is inside the Workouts.get()? Is that using any non-angular asynchronous code? Commented Jul 21, 2016 at 12:49

5 Answers 5

1

If you're using this for an edit feature, make a copy of the object to make edits to so it doesn't affect the original.

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

//you're free to make edits to $scope.workout without affecting the service

In your save function, commit the changes by merging the objects.

angular.merge($scope.workoutTarget, $scope.workout)

Now $scope.workoutTarget will have the edits applied to it.

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

4 Comments

I was typing a similar answer. Will it work? How could you make sure that the angular.copy is called after http get is resolved? I suspect that the $scope.workout will have an empty JSON all the time.
The OP said the get method is returning an object, not a promise.
I assumed that he was using $resource.
I thought it was asynchronous at first too, but his comment on the question says it's returning an object.
1

1 workaround can be, copy your $scope.workout to another Object and assign it back to $scope.workout.

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

var copyObj = angular.copy($scope.workout);

1 Comment

Will it work? How could you make sure that the angular.copy is called after http get is resolved? I suspect that the copyObj will have an empty JSON all the time.
0

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.

4 Comments

I want to get exactly that object what I have hardcoded to my service. How to do that?
@m1crdy see the edited.Can you please show your object ?
Got it: $scope.workout = angular.copy(Workouts.get($scope.workoutId), $scope.workout);
@m1crdy no need to assign to $scope.workout again.
0

While I am not sure where your code is wrong (I am not that great at AngularJS!) A quick hack you can use is this: Assign the initial data to to a new scope variable THEN assign your workout variable from our new scope variable. Now we can refresh workout from our new variable at any time.

Comments

-1

You could try $route.reload to refresh the injected data.

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.