0

我想将身份验证令牌附加到角度 js ngSrc url 请求。那么我怎样才能通过ngSrc指令传递这个令牌呢?

4

3 回答 3

1

ngSrc 内部没有使用 $http,所以单独的拦截器不起作用。它只是设置 src 属性。从我的角度来看,您将不得不编写一个自定义指令,如“ngHttpSrc”,它使用 $http 服务。

请参阅:在动态 ngSrc 请求中强制 HTTP 拦截器

于 2016-05-23T10:29:35.257 回答
1

使用 http-src 而不是 ng-src,它将使用 $http 服务获取图像 - 这意味着将存在通过拦截器添加的授权标头 - 然后构建 Blob 并将 src 设置为 objectURL。

参考:https ://github.com/dougmoscrop/angular-img-http-src

于 2016-05-24T12:00:02.270 回答
0

就像评论中提到的JB一样,使用拦截器

// alternatively, register the interceptor via an anonymous factory
$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
  return {
    'request': function(config) {
      // manipulate the request here
      // You can filter specific requests if you want
      config.headers.token ="whatever";
      return config;
    }
  };
});
于 2016-05-23T07:07:44.810 回答