问题
I see it isn't possible to edit headers with ngRessource after the service has been registered.
Isn't this a big problem for people using authentication via jwt tokens in the autorization header ?
I have to be able too set headers dynamically on each request :
{ Authorization: 'Bearer '+ myAuthService.getToken() }
It poses no problem what so ever with $http.
Is it really impossible to set tokens in the headers via ngRessource after the service has been registered ?
回答1:
I have to be able too set headers dynamically on each request :
To set the header on each request, make the header a function:
{ Authorization: function(config) {
return 'Bearer '+ myAuthService.getToken();
}
}
From the Docs:
- headers –
{Object}
– Map of strings or functions which return strings representing HTTP headers to send to the server. If the return value of a function isnull
, the header will not be sent. Functions accept a config object as an argument.
By furnishing a function, the header value will be computed on each request.
来源:https://stackoverflow.com/questions/39969149/ngresource-dynamically-set-header-on-each-request