Angular 2 - Check if image url is valid or broken

后端 未结 9 1983
感情败类
感情败类 2020-11-29 19:31

I am fetching a large number of image URLs from an API and display them in a angular 2 web application. Some of the URLs are broken and i want to replace them with a default

9条回答
  •  离开以前
    2020-11-29 20:18

    You can simply use a ternary operator for this use case. I am assuming that you are getting the responses from a remote server

    If you don't want to have the image from server write the path in single quotes ''.

    Here if the link is broken then it will go to onErrorFunction if it isn't broken the res.image will be loaded.

提交回复
热议问题