ngresource - How to pass headers on the fly to $resource for angularjs -
right now, way know setting tokens in headers dynamically angularjs call via $http
so:
return $http.get({ url: 'https://my.backend.com/api/jokes', params: { 'jokeid': '5', }, headers: { 'authorization': 'bearer '+ $scope.myoauthtoken } });
but want figure out how pass via $resource, here's pseudo-code doesn't work:
... .factory('myfactory', ['$resource', function($resource){ return { jokes: $resource('https://my.backend.com/api/jokes', null, { query: { method: 'get' } }) }; } ] ); ... return myfactory.jokes.query({ 'jokeid': '5', 'headers': { 'authorization': 'bearer '+ $scope.myoauthtoken } });
how can pass headers on fly $resource angularjs?
i don't think can done way trying, config object not available on action method. action config method has it. can is, rather returning resource directly, create function takes parameter auth token , construct resource , return.
return { jokes: function (token) { return $resource('https://my.backend.com/api/jokes', null, { query: { method: 'get' headers: { 'authorization': 'bearer ' + token } } }) } };
then call service function
myfactory.jokes($scope.myoauthtoken).query({'jokeid': '5'});
Comments
Post a Comment