Search code examples
javascriptjsonionic-frameworkphotos

Ionic: Why are my images not loading from my json file?


I'm trying to load images from my json file into my application but i cannot get it to work:

Here's my code:

js:

.controller('photoCtrl', function($scope, $ionicModal, $ionicBackdrop, $ionicScrollDelegate, $ionicSlideBoxDelegate, $http) {
   $scope.images = [];

    $scope.getImages = function() {
        $http.get('https://api.myjson.com/bins/37ia6')
            .success(function(data) {
                $scope.images = data.images;
            })
    }

html:

<ion-view view-title="Gallery" align-title="center" ng-controller="photoCtrl" >

  <ion-content ng-init="getImages()" class="center" class="has-header padding">

    <!-- start Under6/7/8/9s Photos -->
    <div class="item item-divider">
      <i class="ion-images"></i> Under6/7/8/9s Photos
    </div>
    <a class="item item-list-detail">
      <ion-scroll direction="x">
        <img on-hold="onHold()" ng-repeat="image in images" ng-src="{{images.src}}" ng-click="showImages($index)" class="image-list-thumb" />
      </ion-scroll>
    </a>
</ion-content>
</ion-view>

Solution

  • I think the reason is in your JSON. What would you expect to happen when iterating over:

    {"images":"http://cdn.caughtoffside.com/wp-content/uploads/2012/07/Marko-Marin.jpg"}
    

    Probably your API should return an array of objects like

    [{"src":"http://cdn.caughtoffside.com/.../Marko-Marin.jpg"},
     {"src":"http://cdn.caughtoffside.com/.../Johnny-Blue.jpg"},
     ...
    ]
    

    Iterating over an object usually looks different:

    <div ng-repeat="(key, value) in myObj">