Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In the following code example:

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

    $httpProvider.defaults.withCredentials = true;

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

    return JSON.stringify(data);

}]);

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

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');})
}]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...