I'm currently working on a webpage where I want to show an image only if the URL of that image is valid, using AngularJS.
The challenge I'm facing is that ngIf only checks whether a value is null or not. So, even if the URL returns a 404 error, something will be displayed. However, I want to display the image only if it exists and not if it returns a 404 error.
https://i.stack.imgur.com/WcC2T.png
Is there a way to prevent displaying the image if it returns a 404 error and only display it when it's available?
<img class="menu" ng-if="$ctrl.companyLogo" ng-src="{{ $ctrl.companyLogo}}">
JavaScript code:
$ctrl.companyLogo = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
Currently, instead of showing the default logo when companyLogo returns a 404 error, no image is displayed at all.