AngularJS - 配置 $httpProvider 的未知提供者 [英] AngularJS - Unknown provider configuring $httpProvider

查看:29
本文介绍了AngularJS - 配置 $httpProvider 的未知提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码示例中:

myApp.config(['$httpProvider', function($httpProvider, $cookieStore) {

    $httpProvider.defaults.withCredentials = true;

    $httpProvider.defaults.headers.get['Authorization'] = 'Basic '+ $cookieStore.get('myToken');

    return JSON.stringify(data);

}]);

我收到一个 angularjs 错误,如未知提供者 $cookieStore".

I get an angularjs error like 'Unknown provider $cookieStore'.

'myApp' 有依赖项,'ngCookies' 和 angular-cookies.min.js 已加载,那么该代码有什么问题?

'myApp' has dependenciy and 'ngCookies' and angular-cookies.min.js is laoded, so what's wrong with that code ?

这是我在 .config 中这样做的事实吗?

Is that fact that i'm doing this in .config ?

推荐答案

因为只能在配置时传递提供者,我终于完成了我的 http 参数的覆盖,而不是使用请求转换器,而是通过创建作为工厂的服务来做请求.

Because it's only possible to pass providers when configuring, i have finally done the overwrite of my http parameter not with a request transformer but by creating a service as factory to do requests.

以下是该服务的代码示例(未经测试,仅供参考):

Here is a code example of the service (not tested, just for information):

angular.module('myapp-http-request', []);
angular.module('myapp-http-request')
.factory('MyRequests', function($http, $cookieStore){

    return {
        request: function(method, url, data, okCallback, koCallback){
            $http({
                method: method,
                url: url,
                data: data
            }).success(okCallback).error(koCallback);
        },
        authentifiedRequest: function(method, url, data, okCallback, koCallback){
            $http({
                method: method,
                url: url,
                data: data,
                headers: {'Authorization': $cookieStore.get('token')}
            }).success(okCallback).error(koCallback);
        }
    }
});

以及使用示例(未经测试,仅供参考):

And example of usage (not tested, just for information):

angular.module('sharewebapp', ['myapp-http-request'])
.controller('MyController', ['MyRequests', function(MyRequests){
    MyRequests.authentifiedRequest('DELETE', '/logout', '', function(){alert('logged-out');}, function(){alert('error');})
}]);

这篇关于AngularJS - 配置 $httpProvider 的未知提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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