tic*_*tic 4 javascript angularjs angularjs-directive angular-promise
我没有能力更改端点,如果失败,它将返回200 OK响应,其中包含不同的数据.如何让它运行错误函数(then来自promise 的第二个函数)?
我的服务:
self.getData = function() {
return $http.get('/api/controller/action').then(function(x) {
//This will be run even on failure. How can I call....
return x.data;
}, function(x) {
//......here, from the front-end, so that, the 2nd function in
//inside the 'then' in the controller is run.
//This code will currently never run as it never fails server side.
return x;
});
};
Run Code Online (Sandbox Code Playgroud)
控制器:
MyService.getData().then(function(x) {
//Success
}, function(x) {
//Failure
});
Run Code Online (Sandbox Code Playgroud)
使用$q.reject,例如
return $http.get('/api/controller/action').then(function(x) {
if (x.data === 'some error condition') {
return $q.reject(x);
}
Run Code Online (Sandbox Code Playgroud)