I've been saving all the data received from services direct to local variable, controller, or scope. What I suppose would be considered a shallow copy, is that correct?
Example:
DataService.callFunction()
.then(function(response) {
$scope.example = response.data;
});
Recently I was told to use angular.copy in order to create a deep copy.
$scope.example = angular.copy(response.data);
However, the deep copy information seems to be working in the same way when used by my Angular application. Are there specific benefits to using a deep copy (angular.copy) and can you please explain them to me?
I know its already answered, still i am just trying to make it simple. So angular.copy(data) you can use in case where you want to modify/change your received object by keeping its original values unmodified/unchanged.
For example: suppose i have made api call and got my originalObj, now i want to change the values of api originalObj for some case but i want the original values too so what i can do is, i can make a copy of my api originalObj in duplicateObj and modify duplicateObj this way my originalObj values will not change. In simple words duplicateObj modification will not reflect in originalObj unlike how js obj behave.
Result is like....
I am just sharing my experience here, I used angular.copy() for comparing two objects properties. I was working on a number of inputs without form element, I was wondering how to compare two objects properties and based on result I have to enable and disable the save button. So I used as below.
I assigned a original server object user values to my dummy object say userCopy and used watch to check changes to user object.
my server API which gets me data from server
//initially my save button is disabled because objects are same, once something //changes I am activing save btn
I am not sure but comparing two objects was really headche for me always but with angular.copy() its went smothly.
Javascript passes variables
by reference
, this means that:Now because of
by reference
parti
is [1], andj
is [1] as well, even though onlyi
was changed. This is because when we sayj = i
javascript doesn't copy thei
variable and assign it toj
but referencesi
variable throughj
.Angular copy lets us lose this reference, which means:
Now
i
here equals to [1], whilej
still equals to [].There are situations when such kind of
copy
functionality is very handy.When using angular.copy, instead of updating the reference, a new object is created and assigned to the destination(if a destination is provided). But there's more. There's this cool thing that happens after a deep copy.
Say you have a factory service which has methods which updates factory variables.
and a controller which uses this service,
When the above program is run the output will be as follows,
Thus the cool thing about using angular copy is that, the references of the destination are reflected with the change of values, without having to re-assign the values manually, again.
Use angular.copy when assigning value of object or array to another variable and that
object
value should not be changed.Without deep copy or using angular.copy, changing value of property or adding any new property update all object referencing that same object.
In that case, you don't need to use
angular.copy()
Explanation :
=
represents a reference whereasangular.copy()
creates a new object as a deep copy.Using
=
would mean that changing a property ofresponse.data
would change the corresponding property of$scope.example
or vice versa.Using
angular.copy()
the two objects would remain seperate and changes would not reflect on each other.