首先,您的服务器应与相关的错误返回错误 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是一个 $资源实例, ,那么您不必从服务器再次获得它,$ 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)