function serviceCall() {
var defer =$q.defer();
// make http request
return defer.promise;
}
serviceCall().then(function(){
}).catch(function(err){
//log err
})
如果服务失败,我将获得正确的状态代码,例如404 但有些情况下,错误的对象本身为空
何时将错误对象设为空?
答案 0 :(得分:0)
服务
this.getData=function(){
var deferred = $q.defer();
$http({method:'GET', url:'app/rest/data'})
.success(function(data,status,header,config){
deferred.resolve(data);
})
.error(function(data,status,header,config){
deferred.reject(status);
})
return deferred.promise;
};
控制器中的功能
myService.getData().then(function(data){
$scope.myData = data;
},function(reason){
$scope.errorMessage = reason;
});
从服务器端出现错误消息,如果reason
的长度为零,则您可以编写自己的错误消息,如下所示
myService.getData().then(function(data){
$scope.myData = data;
},function(reason){
if(reason.length <1){
$scope.errorMessage = "Something wrong happned";
} else {
$scope.errorMessage = reason;
}
});