3

Factory code

app.factory('abcFactory', function ($http, Config, $log) {
    var serviceURL = Config.baseURL + '/results';
    return{
        results:function() {
            var promise = $http({
                method: 'GET',
                url: serviceURL,
                timeout: Config.httpTimeout
            }).then(function(response) {
                    return response;  

                },  function(reason) {
                    $log.error("Request Failed: ", reason)
                });

            return promise;
        }
    }
});

Controller code

app.controller('abcController',function ($scope, $log,abcFactory, Config)
{

        abcFactory.results(function(data){
            $scope.results =data;
        },function(response)
        {
          console.log("response"+response);

          if(response.status === 0)
          {
              console.log("gotcha");
               $scope.serviceTimedoutError(response.data, response.status, response.config);
          } else
          {
                console.log("gotcha");
                $scope.serviceFailure(response.data, response.status, response.config);
          }


        });
});

$scope.results populates fine when service has returned good response.

And, when there is an error I can see on console the error message coming from factory code's log saying "Request Failed: blah blah ".

My issue: When there is an error, why I am not seeing error message in controller, it is not even printing "gotcha" in browser console. I need error details in controller, so that I can show it on view. I don't want to pass $scope in factory.

What wrong I am doing in Controller code?

If I am not wrong, I am doing something in line of AngularJS Failed Resource GET but not getting desired result.

2 Answers 2

4

It looks like you want the results function to take callbacks, but its arguments list is currently empty. You are returning the promis though.

So either change the call in the controller to:

abcFactory.results()
  .success(function() { ... })
  .error(function() { ... })

Or change the results function itself:

results: function(successCallback, errorCallback) {
  var promise = $http(...).success(successCallback).error(errorCallback);
  return promise;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I tried your second option. I still don't get error msg in controller. Do you want me to change something in Controller code as well?
See the $http docs: docs.angularjs.org/api/ng.$http. If you're using the success and error callbacks, they receive different arguments than the callbacks to then. [data, status, headers, config].
+1.. though this way it works. But now, I have to shift processing logic of response to controller, which I wanted to keep in factory. I was looking to solve it without having to use .success() and .error() in controller. I will wait for some more responses on this, before accepting your answer.
1

Deprecation Notice The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.