First of all, your server should return errors with relevant HTTP error status codes (see 4xx and 5xx codes). That way, you only handle errors in the error callback:
function onError (response){
switch (response.status) {
case 400:
case 404:
//etc...
response.data.errors.forEach(function(error){
$scope.alerts.push({type:'danger', msg: error.msg});
});
break;
case 500:
$scope.alerts.push({type:'danger', msg: "There was a problem saving your data: " + response.data});
break;
}
}
That said, if $scope.user is a $resource instance, then you do not have to get it again from the server, the $save() method will not change the object.
To copy values from the 'user' object retrieved from the server into the $scope.user just use angular.extend()
angular.extend($scope.user, data) //this updates $scope.user with data attributes.
Is worth noting that angular.extend
does not perform a deep copy, if needed, use jQuery.extend:
jQuery.extend(true, $scope.user, data)