0

How to i handle when there is no response from api

service.js

app.factory('dropdownService', function ($http,CD_CONTEXT_VOLTAGE_URL) {
    return {
        getDropDownData: function (callback) {
            $http.get(CD_CONTEXT_VOLTAGE_URL).success(callback);
        }
    }
});

controller.js

app.controller('loadDropDownController', ['$scope','dropdownService',function($scope,dropdownService) {

    getFormData();
    function getFormData() {
        dropdownService.getDropDownData(function (results) {
            $scope.contextlist = results.context;
            $scope.cdlst = results.coordinationType;
            $scope.voltageList = results.constraintValue;

        });
    };
}]);

The above code handles only success conditions.Could somebody help me with the code so that i can handle the error condition

3 Answers 3

1

If you check angular $http documentation, you will see that $http.get() returns a promise which has an .error() method. That is where you give some function which handles the error for you.

app.factory('dropdownService', function ($http,CD_CONTEXT_VOLTAGE_URL) {
return {
    getDropDownData: function (callback) {
        $http.get(CD_CONTEXT_VOLTAGE_URL).
             success(callback).
             error(errorCallback);
    }
}

});

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

Comments

1

In your service.js

app.factory('dropdownService', function ($http,CD_CONTEXT_VOLTAGE_URL) {
    return {
        getDropDownData: function (callback, errorCallback) {
            $http.get(CD_CONTEXT_VOLTAGE_URL)
            .success(callback)
            .error(errorCallback);
        }
    }
});

While in your controller.js

app.controller('loadDropDownController', ['$scope','dropdownService',function($scope,dropdownService) {

    getFormData();
    function getFormData() {
        dropdownService.getDropDownData(function (results) {
            $scope.contextlist = results.context;
            $scope.cdlst = results.coordinationType;
            $scope.voltageList = results.constraintValue;

        }, function(error){
  console.log('Error while getting response from the REST call');
});
    };
}]);

This should do the trick.

Comments

1

In your service you can register .error callback() as well along with, .success() call back.

The updated code will be:

service.js

app.factory('dropdownService', function ($http,CD_CONTEXT_VOLTAGE_URL) {
    return {
        getDropDownData: function (successcallback,errorCallback) {
            $http.get(CD_CONTEXT_VOLTAGE_URL)
                .success(successcallback)
                .error(errorCallback);
        }
    }
});

controller.js

app.controller('loadDropDownController', ['$scope','dropdownService',function($scope,dropdownService) {

    getFormData();
function getFormData() {
    dropdownService.getDropDownData(function (results) {
        $scope.contextlist = results.context;
        $scope.cdlst = results.coordinationType;
        $scope.voltageList = results.constraintValue;

    },function(){ 
         //do something on error
       });
};

}]);

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.