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(=).
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,
= represents a reference whereas angular.copy() creates a new object as a deep copy.
Using = would mean that changing a property of contextwould change the corresponding property of $scope.master or vice versa.
Using angular.copy() the two objects would remain seperate and changes would not reflect on each other.
When you manipulate primitive types (like int) in Javascript, = and angular.copy are the same as any assignment results in copying the value of the variable.
When you manipulate objects in Javascript, = assign a reference to the existing object to the variable and angular.copy is copying, that means creating a new object with the same properties and values and assigning the new object's reference to the variable.