admin管理员组文章数量:1300042
My data has a property "photo", which may be empty or contain a file name.
For example, "steve.jpg" or "" if steve has no photo.
In React JSX I could use a ternary operator on the value of "photo", but I don't know how to do it with Angular 8.
In know I could use 2 image tags with 2 ngIf
s, but I'd like ot make it more dry. This doesn't work for me at all:
<img[attr.src]="person.photo !=='' ? '/assets/images/people/{{person.photo}}' : '/assets/images/people/missing-person.jpg'">
My data has a property "photo", which may be empty or contain a file name.
For example, "steve.jpg" or "" if steve has no photo.
In React JSX I could use a ternary operator on the value of "photo", but I don't know how to do it with Angular 8.
In know I could use 2 image tags with 2 ngIf
s, but I'd like ot make it more dry. This doesn't work for me at all:
<img[attr.src]="person.photo !=='' ? '/assets/images/people/{{person.photo}}' : '/assets/images/people/missing-person.jpg'">
Share
Improve this question
asked Jul 31, 2019 at 14:57
SteveSteve
14.9k37 gold badges138 silver badges245 bronze badges
1
-
Have you tried to do this without the
attr.
? – Morphyish Commented Jul 31, 2019 at 14:59
2 Answers
Reset to default 8it's just [src] in angular, and the {{ }} syntax isn't valid in bindings. Only in interpolation, just access the variables like normal
<img [src]="person.photo !=='' ? '/assets/images/people/' + person.photo : '/assets/images/people/missing-person.jpg'">
You can do it like :
<img src="/assets/images/people/{{ (person.photo !=='' ? person.photo : 'missing-person') + '.jpg'}}">
本文标签: javascriptAngular 8 ternary operation for img src attributeStack Overflow
版权声明:本文标题:javascript - Angular 8: ternary operation for img src attribute? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741654226a2390661.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论