AngularJS ng-src in ie8: image not loaded and “unsafe” added to path

雨燕双飞 提交于 2019-12-11 04:14:19

问题


I just wanted to add an image to my app. As advised, I used the ng-src directive:

<img ng-src="{{app.imgBig}}" alt="lorem ipsum" height="100" width="200" title="lorem ipsum" />

It works fine on most browsers but it doesn't on IE8. The image is not loaded as instead of adding the absolute url to the file name, it returns the relative path and adds "unsafe:" before it.

Actually, it's trying to load:

unsafe:img/test.jpg

Instead of:

http://url_of_my_site.com/img/test.jpg

So you know, it may be related to the fact I had to completely disable SCE to support IE7:

$sceProvider.enabled(false);

A quick fix was to create a base path constant and to add it at the beginning of the image path but it's not the best as the app shouldn't be dependent from this base path.

Any idea ?


回答1:


Found a solution myself:

  1. Get the current base url thanks to the Location object:

    var base_url = location.protocol + "//" + location.hostname + location.pathname;
    
  2. Give it as a base path to my image in my controller:

    imgBig: base_url + 'img/' + $scope.imgBig
    

That way, unsafe: disappeared, it works on every browsers and it's independent from a constant base path.

Hope it can help !



来源:https://stackoverflow.com/questions/21837851/angularjs-ng-src-in-ie8-image-not-loaded-and-unsafe-added-to-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!