0

I'm getting room images from a 3rd party API as json.

If the room contains images it creates a room image object inside my hotel object, otherwise it does not create the room image object.

I just want to display the first image if it is multiple - Else display an "No image available" image.

Would I be better in Angular to do the if statement before it gets to the view or do a conditional inline in the view? Or use a filter?

I know in php you can use isset. Does angular have something similar?

<img class="img-responsive" ng-src="{{room.RoomImages.RoomImage[0].url}}" />
1
  • {{(room.RoomImages.RoomImage|| [] ).length ? room.RoomImages.RoomImage[0].url : 'your img here'}} Commented Feb 3, 2016 at 8:46

1 Answer 1

0

There is a different approach a directive like this:

return {
    restrict: 'A',
    link: function(scope, element) {
      element.on('load', function() {
        unWatch();
        //image exists
      });
      element.on('error', function() {
        unWatch();
       // fallback image
      });
      var unWatch = scope.$watch('ngSrc', function() {
        //do something ie a loader
      });
    }
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.