防止img src属性中的404错误

时间:2022-11-29 14:12:21

I want to load the thumbnail of video

我想加载视频的缩略图

<img src="http://www.dailymotion.com/thumbnail/video/{{video.id}}">

But until AngularJS is loaded, I get a 404 error

但是在加载AngularJS之前,我将得到一个404错误

GET http://www.dailymotion.com/thumbnail/video/%7B%7Bvideo.id%7D%7D 404 (Not Found)

获取http://www.dailymotion.com/thumbnail/video/%7b%7b%7bvideo.id%7d%7d%7d%7d%7d%7d%7d%7d 404(未找到)

It's not a big deal, but I would like to solve it.

这不是什么大问题,但我想解决它。

1 个解决方案

#1


33  

You should be using ng-src or the image url will be resolved literally. a video like http://www.dailymotion.com/thumbnail/video/4 does exist, but not the literal string http://www.dailymotion.com/thumbnail/video/{{video.id}}

您应该使用ng-src,否则图像url将被逐个解析。像http://www.dailymotion.com/thumbnail/video/4这样的视频是存在的,但是不存在字面字符串http://www.dailymotion.com/thumbnail/video/{{video.id}

I made a jsfiddle demonstrating the img ng-src behaviour. You can see with the network tab in Firebug that you don't get a 404 error.

我做了一个小提琴演示img ng-src行为。通过Firebug中的network选项卡,您可以看到不会出现404错误。

#1


33  

You should be using ng-src or the image url will be resolved literally. a video like http://www.dailymotion.com/thumbnail/video/4 does exist, but not the literal string http://www.dailymotion.com/thumbnail/video/{{video.id}}

您应该使用ng-src,否则图像url将被逐个解析。像http://www.dailymotion.com/thumbnail/video/4这样的视频是存在的,但是不存在字面字符串http://www.dailymotion.com/thumbnail/video/{{video.id}

I made a jsfiddle demonstrating the img ng-src behaviour. You can see with the network tab in Firebug that you don't get a 404 error.

我做了一个小提琴演示img ng-src行为。通过Firebug中的network选项卡,您可以看到不会出现404错误。