まず第一に、サーバーは関連するエラーを返す必要があります HTTPエラーステータスコード (4xxおよび5xxコードを参照)。そうすれば、エラーコールバックのエラーのみを処理します。
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;
}
}
とはいえ、$ scope.userがaの場合 $リソースインスタンス, 、そうすれば、サーバーから再度取得する必要はありません。$ save()メソッドはオブジェクトを変更しません。
サーバーから取得された「ユーザー」オブジェクトから$ scope.userに値をコピーするには、使用するだけです angular.extend()
angular.extend($scope.user, data) //this updates $scope.user with data attributes.
注目に値します angular.extend
必要に応じて、深いコピーを実行しません。 jquery.extend:
jQuery.extend(true, $scope.user, data)