img ng-src图像永无止境

时间:2015-10-02 16:30:01

标签: angularjs html5

我有类似下面的内容

<img width="100" height="177" ng-show="file.fileName" ng-src="file.fullPath" class="img-thumbnail" alt="test" onerror="this.src = $(this).attr('altSrc')">

fileName is "abc.png"
fullPath is "/data/uploads/abc.png"

但是,src永远不会结束..下面是日志。

INFO 2015-10-02 03:02:40-6357939496072691129 - Request: GET localhost:26264/undefined 
INFO 2015-10-02 03:02:40-6357939496072691129 - Response: GET localhost:26264/undefined Not Found 
INFO 2015-10-02 03:02:40-6357939496074282039 - Request: GET localhost:26264/undefined 
INFO 2015-10-02 03:02:40-6357939496074282039 - Response: GET localhost:26264/undefined

如何防止这种情况?

1 个答案:

答案 0 :(得分:2)

<img width="100" height="177" ng-show="file.fileName" ng-src="file.fullPath" class="img-thumbnail" alt="test" onerror="this.src = $(this).attr('altSrc')">

您的onerror将src设置为属性altSrc,但由于您尚未定义altSrc,因此会再次出错并导致循环。

要阻止循环,请定义altSrc。为了防止这种情况,请不要像这样内联javascript。而是在CSS中为您的图像元素定义背景图像。这样您就不需要检查图像错误,因为加载的图像将只覆盖CSS背景图像。