Angularjs 角度$http.reject与$q.reject

Angularjs 角度$http.reject与$q.reject,angularjs,angular-promise,angular-http,Angularjs,Angular Promise,Angular Http,我记得有一段时间我研究过这个问题,结果空手而归,但我仍然找不到任何关于这个问题的好信息——为什么会出现$q.reject方法而不是$http.reject方法 例如,在现实生活中,我们可能有: unfollow: function (userId) { if (!AuthService.isLoggedIn()) { //$location.url('/login'); window.location.href = '/login'; r

我记得有一段时间我研究过这个问题,结果空手而归,但我仍然找不到任何关于这个问题的好信息——为什么会出现
$q.reject
方法而不是
$http.reject
方法

例如,在现实生活中,我们可能有:

unfollow: function (userId) {

    if (!AuthService.isLoggedIn()) {
        //$location.url('/login');
        window.location.href = '/login';
        return $q.reject({error: 'no logged-in user, but non-existent user could still click a follow button?'});
    }
    else {
        return $http({
            method: 'PUT',
            url: ConfigService.api.baseUrl + '/v1/users/add_unfollow/by_id/' + userId
        });
    }

}

我宁愿使用相关的
$http.reject
而不是
$q.reject
,但这似乎不起作用。

因为
$http
在条件的一段中返回承诺…函数本身也需要在另一段中返回承诺

使用
$q.reject()
只是返回被拒绝承诺的快捷方式


如果没有它,任何调用
unfollow().then()
的地方都不会有
then()
方法,如果没有返回承诺

,因为
$http
在条件的一段中返回承诺……函数本身也需要在另一段中返回承诺

使用
$q.reject()
只是返回被拒绝承诺的快捷方式


没有它,任何调用
unfollow().then()
的地方都不会有
then()
方法,如果没有返回承诺

$http将包装http调用并返回承诺。如果实际的http请求失败,承诺将被拒绝,添加拒绝方法没有多大意义,而是应该在承诺中


在您的示例中,您甚至不需要调用$http服务来拒绝请求

$http将包装http调用并返回承诺。如果实际的http请求失败,承诺将被拒绝,添加拒绝方法没有多大意义,而是应该在承诺中


在您的示例中,您甚至不需要调用$http服务来拒绝请求

您从未打过
$http
电话,因此
$http
不会拒绝您的承诺。这里的操作方式是一种很好的方法。您希望
$http.reject
是什么样的呢?您希望所有返回承诺的函数都有
.reject
方法吗?例如
unfollow.reject()
?您从未进行过
$http
调用,因此
$http
不会拒绝该承诺。这里的操作方式是一种很好的方法。您希望
$http.reject
是什么样的呢?您希望所有返回承诺的函数都有
.reject
方法吗?例如
unfollow.reject()