Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between angular.copy() and an assignment (=)?

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(=).

like image 342
Ramesh Rajendran Avatar asked Apr 20 '15 13:04

Ramesh Rajendran


People also ask

What does angular copy do?

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.

What's alternative to angular copy in angular?

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 .

Is angular copy a deep copy?

Example# The angular. copy function takes an object, array or a value and creates a deep copy of it.


1 Answers

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,

like image 146
Anders R. Bystrup Avatar answered Sep 20 '22 17:09

Anders R. Bystrup