0

I am currently writing an Angular application that communicates with the flickr API. In order to utilise the response from flickr you need to define a jsonFlickrFeed callback function. Please see answer here for details on how this works

Everything is working fine in my angular app. I am getting the response and showing the data on the screen.

However, to improve UX I would like to show an error message to the user if the data does not load, but I cannot figure out how to display this message. The way I thought it would work, always logs an error the way the API works. Please see my attempt below:

app.controller('testCtrl', function (getFlickrData) {
   var testCtrl = this;
   this.loading = true;
   this.error = false;

   //call to get the data from Flickr
   getFlickrData.getData().catch(function (err) {
       //show error
       console.log(err)
       testCtrl.error = true;
   })
   .finally(function () {
     // Hide loading spinner whether our call succeeded or failed.
     testCtrl.loading = false;
   });

   //API requires jsonFlickrFeed callback function
   jsonFlickrFeed = function(data){
     console.log(data);
   } 
});

and here is the factory I am using to make the GET request to Flickr

app.factory('getFlickrData', function($http){
    return{
        getData: function(){
            return $http.jsonp('https://api.flickr.com/services/feeds/photos_public.gne?tagmode=all&format=json');
        }
    }
});

The err logged in the console looks like this:

{data: undefined, status: 404, config: Object, statusText: "error"}

and finally here is the html which is always shown, even if the content loads successfully.

<div ng-show="testCtrl.error" class="error">
   <p>Sorry but there has been an error with loading content. Please try again soon.</p>
</div>

I guess to sum up my question is how to check success/failure of a callback (possibly)

Here is a jsfiddle that may make it easier to see the problem I am trying to solve

8
  • Does something like this help? jsfiddle.net/zkqrcbxn/1 Commented Mar 11, 2016 at 23:56
  • unfortunately not, it is still giving an error with the api even when it is successful, as there is an error before the callback Commented Mar 12, 2016 at 0:19
  • Should the ng-show="ctrl.error" be ng-show="testCtrl.error == true"? Commented Mar 12, 2016 at 0:22
  • @KennyE thanks for the suggestion. Unfortuantely that's not it, i changed the var names for the question, and ng-show will show if its true, you dont have to state condition === true. If you try the code and inspect the err in the console you will understand the issue Commented Mar 12, 2016 at 0:25
  • testCtrl needs to be on the $scope object too. The convention is to inject $scope into the controller then do $scope.testCtrl.error = true. Is there a reason you are not using $scope in the controller? Commented Mar 12, 2016 at 0:56

1 Answer 1

1

Hope this wont fail you anymore :)

According to AngularJS API in $http there was no finally only success and error can be used here.

app.controller('testCtrl', function (getFlickrData) {
   var testCtrl = this;
   this.loading = true;
   this.error = false;

   getFlickrData.getData().success(function (data) {
       console.log(data);
       testCtrl.loading = false;
   })
   .error(function () {
     testCtrl.loading = false;
     testCtrl.error = true;
   });

})
.factory('getFlickrData', function($http){
    return{
        data:{},
        getData: function(){
            var self = this;
            return $http.jsonp("https://api.flickr.com/services/feeds/photos_public.gne?tagmode=all&format=json&jsoncallback=JSON_CALLBACK");
        }
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

I dont think this works. Do you not get an error message in the console?
Sorry about that should be fixed
I still don't think so. If you look in the console you will see that the err message is still been shown. I need to check the success of the callback if that is possible
Try it out now should be ok
That worked. would you mind explaining in a little more detail how it works in the answer. some extra explanation would be great
|

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.