I am encountering a challenge when trying to update a reference of an object within my code. Here's the scenario:
function modifyUserDetails(user) {
$scope.initialUser = user;
$scope.alteredUser = angular.copy(user);
}
function ApplyUserChanges(form) {
if (form.$valid) {
$scope.initialUser = angular.copy($scope.alteredUser);
}
}
So far, everything is functioning as expected. I utilize modifyUserDetails(user) within an ng-repeat="user in users" loop to pass in the user object. When ApplyUserChanges is triggered, the initialUser object is successfully updated.
Initially, I assumed that since $scope.initialUser = user;
, updating $scope.initialUser would also result in the user object within the users array getting updated, as I thought they shared the same reference. However, upon inspecting with ng-inspect, I noticed that although $scope.initialUser gets updated, the user within the users array remains unchanged.
Any insights on this behavior would be greatly appreciated.