How to write the ternany condition for <img>
src
in Angular 2.
Below is the code I tried but this is not working
<img class="lib-img" [src]="item.pictureUrl!= null ? item.pictureUrl : ~/images/logo.png" height="500" width="500" alt="default image">
Maybe better way is to keep your images in assets folder: assets/img/logo.png
This will do the trick
For more binding information follow this link
https://angular.io/guide/template-syntax#html-attribute-vs-dom-property
For those looking to conditionally provide the
src
attribute to the<img>
element:Use Attribute Binding:
If the expression
item.pictureUrl
evaluates to null, thesrc
attribute will be removed from the<img>
elementIf you want to use variable in image
#svg
to get sourcesvg.getAttribute('src')
, you need implement method to get image in ts file instead of if else in html file.I shared for whom concerned.
TS file
then in your .ts
.html
.ts