The application I'm building requires my user to set 4 pieces of information before this image even has a chance of loading. This image is the center-piece of the application, so the broken image link makes it look like the whole thing is borked. I'd like to have another image take its place on a 404.
Any ideas? I'd like to avoid writing a custom directive for this.
I was surprised that I couldn't find a similar question, especially when the first question in the docs is the same one!
It's a pretty simple directive to watch for an error loading an image and to replace the src. (Plunker)
Html:
Javascript:
If you want to display the error image when ngSrc is blank you can add this (Plunker):
The problem is that ngSrc doesn't update the src attribute if the value is blank.
I use something like this, but it assumes that team.logo is valid. It forces default if "team.logo" isn't set or is empty.
Came up with my own solution. It replaces image both if src or ngSrc is empty, and if img returns 404.
(fork of @Darren solution)
To expand Jason solution to catch both cases of a loading error or an empty source string, we can just add a watch.
Html:
Javascript:
You don't need angular for that, or even CSS or JS. If you want, you can wrap this answer (linked) in a simple directive to make it simpler, like or something, but it's a pretty simple process... just wrap it in an object tag...
How to hide image broken Icon using only CSS/HTML (without js)
in HTML :