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

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

问题描述

在以下code例如:

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'有dependenciy和ngCookies'和角cookies.min.js是laoded,有啥不妥code?

'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.

下面是服务(未测试,只为信息)的code例如:

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天全站免登陆