【发布时间】:2013-11-08 01:12:26
【问题描述】:
我正在使用角度资源尝试使用角度 $save 函数。
默认情况下 $save 将模型发送回服务 URL。但是,它看起来希望模型返回到服务(如果我不这样做,我的模型是空的)。我想知道向控制器返回消息和错误的最佳方式是什么。
我的解决方案是在我的 PHP 中创建一个新类,该类有一个错误数组,用于存储处理过程中遇到的任何错误,以及一个存储模型以供返回的字段。然后在回调函数中发回并处理:
$scope.ApplyChanges=function(){
console.log("saving...");
$scope.user.$save(function(data){
console.log(data);
if (data.errors.length>0){
for (error in data.errors){
$scope.alerts.push({type:'danger', msg: data.errors[error].msg});
}
$scope.user=User.get({id:data.data.UserName});
} else {
$scope.user=User.get({id:data.data.UserName});
$scope.alerts.push({type:'success', msg: "User data saved successfully"});
}
}, function(err){
$scope.alerts.push({type:'danger', msg: "There was a problem saving your data: " + err});
});
这里的这些行:$scope.user=User.get({id:data.data.UserName}); 我必须使用,因为如果我只是将我的 $scope.user 分配给 data.data,则用户不再使用该服务,当我再次尝试 ApplyChanges 时会出现错误。
那么,有没有办法更顺畅地做到这一点?因为它是我必须打一个额外的电话来获取模型。我是否应该仅在出现错误时才发送错误,然后再通过额外的回调来获取模型?有没有更好的办法?
【问题讨论】:
-
想解释否决票?
标签: php angularjs angularjs-resource