Reputation: 857
I have two objects. One is the master data, another is a similar object but contains certain properties that I am using and is a subset of the master data. Please find below the two objects:
$scope.masterData = {
"StoresForOrgs": {
"PPP0001188": ["007071","007073","007079"],
"PPP0001189": ["007075","0070756","0070789"],
"PPP0001190": ["007075","0070756","0070789", "00707893", "00707899"]
}
}
$scope.masterDataForDisplay = {
"StoresForOrgsDisplay": {
}
}
If in my code I do
$scope.masterDataForDisplay = $scope.masterData;
this is linking both the objects and if I change the masterDataForDisplay it is changing the masterData as well. Now I understand that comparing these two would create the same reference and this could be avoided by using then
try this
$scope.masterDataForDisplay = JSON.parse(JSON.stringify($scope.masterData));
but even when the code is not executed this is happening. Are the objects being initialized before?
Upvotes: 0
Views: 29
Reputation: 23211
use this:
$scope.masterDataForDisplay = angular.copy($scope.masterData);
instead of
$scope.masterDataForDisplay = $scope.masterData;
copy() : Creates a deep copy of source, which should be an object or an array. So they dont share the same reference
Upvotes: 1