ngResource 在每个请求上动态设置 Header

ngResource Dynamically Set Header on Each Request

我发现在注册服务后无法使用 ngRessource 编辑 headers。

对于在授权中通过 jwt 令牌使用身份验证的人来说,这不是一个大问题 header 吗?

我也必须能够根据每个请求动态设置 headers :

  {     Authorization: 'Bearer '+ myAuthService.getToken()     }

$http 从来没有问题。

注册服务后真的无法通过ngRessource在headers中设置token吗?

I have to be able too set headers dynamically on each request :

要在每个请求上设置 header,使 header 成为一个函数:

{ Authorization: function(config) {
                     return 'Bearer '+ myAuthService.getToken(); 
                 }
}

来自文档:

  • 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 is null, the header will not be sent. Functions accept a config object as an argument.

通过提供函数,header 值将根据每个请求进行计算。