In Angular 1.2.X
In the realm of Angular 1.2, there exist numerous methods to achieve this task. To streamline the process in Angular 1.2, I suggest utilizing an http interceptor to sanitize outgoing requests and append headers.
// Implementing an interceptor as a service.
app.factory('myInterceptor', function($q) {
return {
// Intercepting requests on their way out.
request: function(config) {
var myDomain = "http://whatever.com";
// (optional) If the request is directed towards your target domain,
// THEN include your header, otherwise leave it as is.
if(config.url.indexOf(myDomain) !== -1) {
// Add the Authorization header (or custom header) here.
config.headers.Authorization = "Token 12309123019238";
}
return config;
}
}
});
app.config(function($httpProvider) {
// Link the interceptor by name in the configuration.
$httpProvider.interceptors.push('myInterceptor');
});
In Angular 1.0.X
If working with Angular 1.0.X, you will have to set the headers more globally in the common headers...
$http.defaults.headers.common.Authentication
EDIT: Dealing with
To achieve this, you will have to craft a directive, and things might get a bit complex.
You will need to:
- Create a directive that is attached to your
<img/>
tag, or generates it.
- Ensure that the directive utilizes the
$http
service to request the image (thus utilizing the aforementioned http interceptor). You will have to analyze the extension and set the appropriate content-type header, for instance: $http({ url: 'images/foo.jpg', headers: { 'content-type': 'image/jpeg' }).then(...)
- Upon receiving the response, you must extract the raw base64 data and assign it to the
src
attribute of your image element as a data source like this: <img src="data:image/jpeg;base64,9hsjadf9ha9s8dfh...asdfasfd"/>
.
... this process may become intricate.
It would be advantageous if your server does not secure the images.