How to pass headers on the fly to $resource for angularjs

让人想犯罪 __ 提交于 2019-11-30 04:52:48

I don't think this can be done the way you are trying, as the config object is not available on action method. But the action config method has it. So what you can do is, rather than returning resource directly, create a function that takes a parameter the auth token and then construct the resource and return.

return {
    jokes: function (token) {
        return $resource('https://my.backend.com/api/jokes', null, {
            query: {
                method: 'GET'
                headers: {
                    'Authorization': 'Bearer ' + token
                }
            }
        })
    }
};

Then call the service function as

myFactory.jokes($scope.myOAuthToken).query({'jokeId': '5'});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!