Aniket Patil
Aniket Patil

Reputation: 37

How to override angular ngSrc directive to append request header?

I want to append a authentication token to the angular js ngSrc url request. So how can I pass this token with ngSrc directive?

Upvotes: 0

Views: 1577

Answers (3)

Gitaram Kanawade
Gitaram Kanawade

Reputation: 351

Use http-src instead of ng-src and it will fetch images using the $http service - meaning Authorization headers added via interceptors will be present - then build a Blob and set the src to an objectURL.

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

Upvotes: 2

ChrisY
ChrisY

Reputation: 1783

ngSrc is not using $http internally, so the interceptor alone will not work. It just sets the src-attribute. From my point of view you will have to write a custom directive like"ngHttpSrc", which is using the $http services.

see: Force HTTP interceptor in dynamic ngSrc request

Upvotes: 1

T J
T J

Reputation: 43156

Like JB mentioned in comments, use an interceptor

// 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;
    }
  };
});

Upvotes: 0

Related Questions