0

I am using API from a well known travel company and I am having this problem.

The problem is the response for roomAvailability.

Now if certain hotel has only one Room Type, it sends it as a Room object rather than sending it as an array which otherwise it would send if there were multiple Room Types

Now in my template, I have a ng-repeat like :

<div ng-repeat='room in Rooms'>
Do some stuff like room.RoomType
</div>

Now what happens is , when Room is not an array , ng-repeat fails as it doesn't iterate over the Room array.

Now what I can do is have a scope variable canIterate and repeat the code in my markup.

But is there a better way so that I can avoid this repeat of code in markup ?

Deepankar Bajpeyi
  • 5,661
  • 11
  • 44
  • 64

1 Answers1

0

Check HTTP response before putting it to $scope.Rooms. If response has field RoomType, declare $scope.Rooms = [response]; otherwise $scope.Rooms = response;

OZ_
  • 12,492
  • 7
  • 50
  • 68
  • 1
    Agreed. How to check if response is an array -> http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript – Rei Mavronicolas Jan 09 '14 at 08:29