0

I'm creating a mobile application using the framework ionic which is built on AngularJS.

I'm trying to load an image into my application from a JSON file but I cannot get it to load correctly.

Could anybody help me out?

Here's my HTML:

<ion-view view-title="Gallery" align-title="center" ng-controller="photoCtrl" ng-init="getImages()">
    <ion-content>
        <div class="col col-25" ng-repeat="image in images">
            <img ng-src="{{images}}" width="100%" />
        </div>
    </ion-content>
</ion-view>

Here's my javascript:

.controller("photoCtrl", function($scope, $http) {     
    $scope.data = [];

    $scope.getImages = function() {
        $http.get('https://api.myjson.com/bins/30vuu')
            .success(function(data) {
                $scope.data = data;
            })
            .error(function() {
                alert("Not working");
            });
    };
});
4
  • What is the error you're getting in console? Commented Jul 6, 2015 at 11:45
  • Also ng-repeat="image in images" to ng-repeat="image in data" Commented Jul 6, 2015 at 11:47
  • I'm not getting any errors in the console. I've changed image to data and still can't get it to work. Should it be images in data? Commented Jul 6, 2015 at 12:06
  • @smither123 @Icycool - It's ng-repeat="image in data.images" but only if the API return is fixed. See my answer below. Commented Jul 6, 2015 at 12:18

2 Answers 2

1

There are several issues here. First, your API call returns an object with a single property called images (not an array). Second, you're not accessing it in your HTML because that would be {{ data.images }} since your $scope member is called data.

So, the first thing you need to do is have the API return an array. If you do that, then use this HTML:

<ion-view view-title="Gallery" align-title="center" ng-controller="photoCtrl" ng-init="getImages()">
    <ion-content>
        <div class="col col-25" ng-repeat="image in data.images">
            <img ng-src="{{image}}" width="100%" />
        </div>
    </ion-content>
</ion-view>

And this JavaScript:

.controller("photoCtrl", function($scope, $http) {
    $scope.getImages = function() {
        $http.get('https://api.myjson.com/bins/30vuu')
            .success(function(data) {
                $scope.data = data;
            })
            .error(function() {
                alert("Not working");
            });
    };
});

If you only want your API to return one image then it's as simple as this for the HTML:

<ion-view view-title="Gallery" align-title="center" ng-controller="photoCtrl" ng-init="getImages()">
    <ion-content>
        <div class="col col-25">
            <img ng-src="{{ data.images }}" width="100%" />
            <!-- data.images because that's the name returned by the API call -->
        </div>
    </ion-content>
</ion-view>

The JavaScript doesn't need to change from what I have above;

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. I have created a codepen: codepen.io/beefman/pen/KpoNjz I still cannot get it to work. I would like the api to return many images but this is just an example json.
@smither123 That's because your API call still returns an object with a property called images that is a string and not an array. codepen.io/anon/pen/GJxrob
1

Please change

<img ng-src="{{image}}" width="100%" />

instead of

ng-src="{{images}}"

2 Comments

Change <img ng-src="{{images}}" width="100%" /> to <img ng-src="{{image}}" width="100%" /> ??
@smither123 you are looping images with ng-repeat and image is the value.

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.