I want to assign some values when a button click event happens via event parameter:
$scope.update = function(context) { $scope.master = context; };
I have assigned user
values to $scope.master
.
Now i am seeing angular.copy(). So I wrote the code with angular.copy.
$scope.update = function(context) { $scope.master = angular.copy(context) };
Both are doing same, so what is the difference? Please tell me about the difference between angular.copy()
and equal(=)
.
You use angular. copy() to an object to prevent other code from modifying it. The original object might change, but your copy won't see the changes. You can reinstate the copy if needed.
The alternative for deep copying objects having nested objects inside is by using lodash's cloneDeep method. For Angular, you can do it like this: Install lodash with yarn add lodash or npm install lodash .
Example# The angular. copy function takes an object, array or a value and creates a deep copy of it.
As can be read here angular.copy()
performs a deep copy (cf. "clone") of the argument - essentially creating a new object - whereas using the assignment operator =
just assigns reference's.
Thus in the latter case, if you we're to change something in $scope.master
you would also change context
.
Cheers,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With