如何在AngularJS中定义错误回调? [英] How to define an error callback in AngularJS?

查看:114
本文介绍了如何在AngularJS中定义错误回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中:

function login(credentials) {
  AuthService
    .login(credentials)
    .then(successCallback, errorCallback);
    //same issue with .then(successCallback).catch(errorCallback);
}

function successCallback() {
  // do something after success
}

function errorCallback(data) {
  // do something after error
}

和在我的中AuthService

authService.login = function (credentials) {
  return $http
    .post(ENV.apiEndpoint + 'api/v1/login_check', credentials)
    .then(
       function (result) {
        Session.create(result.data.token, result.data.data);
       },
       function (data) {
        Messages.create('Login failed: ' + data.statusText);
       }
    );
}

当我 POST 会提供200个响应代码,一切都按预期工作成功执行后要做的事

When my POST delivers a 200 response code, everything works as expected do something after success is executed.

但是当我的POST结果出现时例如在401中,我可以看到Messages.create被调用了(因此在这种情况下,它输入了错误路径),但是不幸的是我的控制器调用了 successCallback 而不是 errorCallback

But when my POST results e.g. in a 401 I can see that Messages.create is called (so in this case it enters the error path), but unfortunately my Controller calls the successCallback and not the errorCallback.

我必须迁移它,因为我正在使用不推荐使用,并且由于Angular 1.6删除了 .success .error Promise属性。当时可以正常使用,但是迁移后不再起作用。

I had to migrate this because I was using the deprecated and since Angular 1.6 removed .success and .error promise attributes. It was working back then, but after migration this doesn't work anymore.

我在这里做什么错了?

推荐答案

您可以在错误回调中拒绝许诺。

You may reject the promise in your error callback.

authService.login = function (credentials) {
  return $http
    .post(ENV.apiEndpoint + 'api/v1/login_check', credentials)
    .then(
       function (result) {
         Session.create(result.data.token, result.data.data);
       },
       function (data) {
         Messages.create('Login failed: ' + data.statusText);
         return $q.reject(data);
       }
    );
}






来自角度$ q文档


reject(reason);

创建一个承诺,该承诺被解决为被拒绝指定的
原因。此api应该用于在
承诺链中转发拒绝。如果您要处理承诺链中的最后一个承诺,则不必担心

Creates a promise that is resolved as rejected with the specified reason. This api should be used to forward rejection in a chain of promises. If you are dealing with the last promise in a promise chain, you don't need to worry about it.

在比较延期/承诺和熟悉的承诺时
try / catch / throw的行为,请将拒绝视为JavaScript中的 throw 关键字。
这也意味着,如果您通过承诺错误
回调捕获错误,并且想要将该错误转发给从当前承诺中的
派生的承诺,则必须重新抛出通过返回通过 reject 构造的
拒绝来返回错误。

When comparing deferreds/promises to the familiar behavior of try/catch/throw, think of reject as the throw keyword in JavaScript. This also means that if you "catch" an error via a promise error callback and you want to forward the error to the promise derived from the current promise, you have to "rethrow" the error by returning a rejection constructed via reject.

这篇关于如何在AngularJS中定义错误回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆