在角1.2.X
有很多方法可以做到这一点。在角度1.2中,我建议使用http interceptor“擦除”传出请求并添加标题。
// An interceptor is just a service.
app.factory('myInterceptor', function($q) {
return {
// intercept the requests on the way out.
request: function(config) {
var myDomain = "http://whatever.com";
// (optional) if the request is heading to your target domain,
// THEN add your header, otherwise leave it alone.
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) {
// wire up the interceptor by name in configuration
$httpProvider.interceptors.push('myInterceptor');
});
在角1.0.X
如果您使用Angular 1.0.X,则需要在标头中更全面地设置标题… $ http.defaults.headers.common.Authentication
编辑:来自的东西
为此,您需要创建一个指令,这可能会变得奇怪。
您需要:
>创建一个指令,该指令位于您的< img />标记,或创建它。
>该指令使用$ http服务请求映像(从而利用上述http拦截器)。为此,您将需要检查扩展名并设置适当的内容类型标题,如:$ http({url:’images / foo.jpg’,headers:{‘content-type’:’image / jpeg’})然后(…)
>当您得到响应时,您必须使用raw base64数据,并将image元素的src属性设置为数据src,如下所示:< img src =“data:image / jpeg; base64,9hsjadf9ha9s8dfh .. .asdfasfd“/取代。
所以会变得疯狂。
如果可以使您的服务器不会保护您更好的图像。