0

I'm trying to create an ionic application which uses a gallery. I'm unsure why my image isn't being loaded into the application.

What i'm trying to develop is an gallery which loads the images from a JSON file.

Here's my HTML

<ion-view view-title="Gallery" align-title="center">

<ion-content ng-controller="photoCtrl" ng-init="getImages()">
    <div class="row" ng-repeat="image in images" ng-if="$index % 4 === 0">
        <div class="col col-25" ng-if="$index < images.length">
            <img ng-src="{{data.images[$index].src}}" width="100%" />
        </div>
        <div class="col col-25" ng-if="$index + 1 < images.length">
            <img ng-src="{{data.images[$index + 1].src}}" width="100%" />
        </div>
        <div class="col col-25" ng-if="$index + 2 < images.length">
            <img ng-src="{{data.images[$index + 2].src}}" width="100%" />
        </div>
        <div class="col col-25" ng-if="$index + 3 < images.length">
            <img ng-src="{{data.images[$index + 3].src}}" width="100%" />
        </div>
    </div>
</ion-content>
</ion-view>

Javascript:

.controller("photoCtrl", function($scope, $http) {

    $scope.images = [];

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

});

I have also created a codepen: http://codepen.io/beefman/pen/eNMgzG

3
  • demo isn't returning an array of images, just an object with a property images that contains a string. Also in your $http callback you aren't doing anything with the response data so you are assigning $scope.data to empty array Commented Jul 8, 2015 at 13:29
  • html doesn't match data structure either Commented Jul 8, 2015 at 14:24
  • My Javascript should be $scope.images = data? Also i'm unsure of why my html doesn't match data sctructure. When i use <div class="col col-25"><img ng-src="{{data.images}}" width="100%"/></div>. It works? Commented Jul 8, 2015 at 14:33

2 Answers 2

1

The JSON API return an associative array (key-value pair) like this:

{"images":"http://mintywhite.com/wp-content/uploads/2012/10/fond-ecran-wallpaper-image-arriere-plan-hd-29-HD.jpg"}

but your template code expects an array (list):

data.images[$index].src

so I would start by matching your JSON with the format expected by your template.

Edit: Here's a JSON format that would match your template's expectations:

{'images': [{'src': 'url1'}, {'src': 'url2'}]}

Also, make sure to set the images variable properly in your callback

$scope.images = data.images

now $scope.images is a list of key value pairs

with ng-repeat you can display the images like so:

<div ng-repeat="image in images">
  <img ng-src="image.src" />
</div>
Sign up to request clarification or add additional context in comments.

5 Comments

How do i change my JSON format to match my HTML? "photos":[ {"images":"url"} ] - something like that? then in my html it would be photos.data.images?
Still can't get it to work. I've tried changing my json file by adding an array. In my HTML do i have to include the array? codepen.io/beefman/pen/eNMgzG
I see that the codepen is working now (sorta), do you still have issues?
Yes, for some reason the forth image isn't loading. No idea why. Something to do with my HTML i'm guessing. A part from that it is working. I've checked my JSON file and it's fine.
Fixed it now. Missing a }
1

Your controller should be like this

.controller("photoCtrl", function($scope, $http) {

    $scope.images = [];

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

});

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.