Nisha shetty
Nisha shetty

Reputation: 121

Error handling in Angular js

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

Upvotes: 0

Views: 74

Answers (3)

Naruto
Naruto

Reputation: 708

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
       });
};

}]);

Upvotes: 1

Vaibhav Pachauri
Vaibhav Pachauri

Reputation: 2671

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.

Upvotes: 1

MantasG
MantasG

Reputation: 111

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);
    }
}

});

Upvotes: 1

Related Questions