admin管理员组文章数量:1334906
i am looping through a list of things, and if a thing has an image i want to show it.
but if a thing does not have an image, i want to display default text:
<div ng-repeat="myThing in myThings>
<div ng-switch on="{{myThing.img}}" >
<span ng-switch-when="{{myThing.img}}">
<img src="/img/myThings/{{myThing.id}}" />
</span>
<span ng-switch-default>No Image</span>
</div>
</div>
this works and the default text is displayed.
however, the browser is also throwing an error trying to get an image which does not exist:
http://localhost:9000/img/myThings/%7B%7BmyThing.id%7D%7D 404 (Not Found)
is there a way to format this ng-switch function so that this error is not thrown?
i am looping through a list of things, and if a thing has an image i want to show it.
but if a thing does not have an image, i want to display default text:
<div ng-repeat="myThing in myThings>
<div ng-switch on="{{myThing.img}}" >
<span ng-switch-when="{{myThing.img}}">
<img src="/img/myThings/{{myThing.id}}" />
</span>
<span ng-switch-default>No Image</span>
</div>
</div>
this works and the default text is displayed.
however, the browser is also throwing an error trying to get an image which does not exist:
http://localhost:9000/img/myThings/%7B%7BmyThing.id%7D%7D 404 (Not Found)
is there a way to format this ng-switch function so that this error is not thrown?
Share Improve this question asked Jul 29, 2013 at 19:32 SeanPlusPlusSeanPlusPlus 9,05318 gold badges62 silver badges84 bronze badges1 Answer
Reset to default 8Use ng-src instead of src
, this is from the doc:
Using Angular markup like {{hash}} in a src attribute doesn't work right: The browser will fetch from the URL with the literal text {{hash}} until Angular replaces the expression inside {{hash}}. The ngSrc directive solves this problem.
<img ng-src="/img/myThings/{{myThing.id}}" />
本文标签: javascriptAngular JS using ngswitch and checking if file existsStack Overflow
版权声明:本文标题:javascript - Angular JS: using ng-switch and checking if file exists - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742331527a2454790.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论