1

Is there a way to detect a request error in Angular 2 property bindings that make Http requests, like [link] and [src].

My situation is I have an object with

{
  local: '/images/abc.jpg',
  remote: 'http://linkToImage.jpg'
}

And the template

<img [src]="local">

So I'm trying to point to remote when local returns a 404 or request error.

gngchrs
  • 468
  • 4
  • 11
  • Got around it thanks to @amit-suhag, [http://stackoverflow.com/questions/36429903/angular-2-check-if-image-url-is-valid-or-broken] http://stackoverflow.com/questions/36429903/angular-2-check-if-image-url-is-valid-or-broken – gngchrs Dec 01 '16 at 06:59

1 Answers1

0

I am not sure if this work or not but can you try doing this with ternary operators

<img src="{{enquiry.buyer_email ? '/images/ico_yes.png' : '/images/ico_no.png'}}">

Or something like this with binary operators:

<img src="{{enquiry.buyer_email && '/images/ico_yes.png' || '/images/ico_no.png'}}">

replace values with remote and local

Amit kumar
  • 6,029
  • 6
  • 30
  • 40
  • ternary operator will look for a falsy value. In this case, the value is not falsy, it is defined. The only difference is that it is a path that returns a 404 not found when called via http. – gngchrs Nov 30 '16 at 18:38
  • 1
    Listen to the error event of the image element: .. check out this link http://stackoverflow.com/questions/36429903/angular-2-check-if-image-url-is-valid-or-broken – Amit kumar Nov 30 '16 at 18:47