最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs image not showed : unsafe:background-image - Stack Overflow

programmeradmin0浏览0评论

I want to show an image in my Angularjs application here is the code :

<img  src="background-image: url({{image.url}})" />

Where image.url is a scope variable with the value http://localhost/assets/photo/41/photo.jpg hosted on an apache server on localhost.

The GET request fail with the error :

unsafe:background-image: url(http://localhost/assets/photo/41/photo.jpg):1
GET unsafe:background-image: url(http://localhost/assets/photo/41/photo.jpg) 
net::ERR_UNKNOWN_URL_SCHEME

Thanks in advance,

I want to show an image in my Angularjs application here is the code :

<img  src="background-image: url({{image.url}})" />

Where image.url is a scope variable with the value http://localhost/assets/photo/41/photo.jpg hosted on an apache server on localhost.

The GET request fail with the error :

unsafe:background-image: url(http://localhost/assets/photo/41/photo.jpg):1
GET unsafe:background-image: url(http://localhost/assets/photo/41/photo.jpg) 
net::ERR_UNKNOWN_URL_SCHEME

Thanks in advance,

Share Improve this question edited Jan 9, 2016 at 5:13 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked May 4, 2015 at 16:18 Master MindMaster Mind 3,0944 gold badges35 silver badges65 bronze badges 3
  • Try this: <img ng-src="image.url" /> – nikola-miljkovic Commented May 4, 2015 at 16:20
  • @nikolaMM94 not only ng-src="image.url" it would be in {{}} interpolation – Pankaj Parkar Commented May 4, 2015 at 16:28
  • 1 Oh sorry i didn't really pay attention, yeah you are right. – nikola-miljkovic Commented May 4, 2015 at 16:30
Add a ment  | 

1 Answer 1

Reset to default 6

You are using img tag incorrectly

<img  ng-src="{{image.url}" />

& your image url ing from server would be /assets/photo/41/photo.jpg instead of http://localhost/assets/photo/41/photo.jpg, Actual domain is not required when you are working on same domain.

Just not clarified what you want to do..It also seems like you may want to use background-image in that case ng-style would be helpful

<div ng-style="{'background-image': 'url('+ image.url+')' }"></div>

Update

As URL is of other domain & you wanted to show it in do that url as trustedResourceURL using $sce service

$scope.getTrustedResourceUrl = function(url){
   return $sce.getTrustedResourceUrl(url)
};

HTML

And get other domain URL to get working it without prepending unsafe: before url then you need to sanitize imgSrc in config & whitelist url regx in it.

app.config(function($pileProvider) {
    var imgSrcSanitizationWhitelist = /^\s*(https?|ftp|file):|data:image\//;
    $pileProvider.imgSrcSanitizationWhitelist(imgSrcSanitizationWhitelist);
});
发布评论

评论列表(0)

  1. 暂无评论