Search code examples
angularimagestring-interpolation

How do I display a default source if the first one fails?


I got this case where I am trying to display a general image if the image doesn't exit on the server.

I want to make it using ternary operator, but the "src" doesn't apply to the second condition.

<div class="test-picture">
   <img src="{{(test?.picture)? test.picture : '/images/test.png'}}" alt="test Image">
</div>


Solution

  • if you are using Angular, you should have a look at

    <img [src]="myPath"/>
    

    instead of <img src="">

    https://angular.io/guide/template-syntax