Reputation: 7083
I am using angularJs factory its making call and working as expected but in terms of exception i have question what if we have exception after making call and it fails so in this case when i execute prcDeleteFactory.deletePrcInventory
i want to check first if there is any error display error and stay on same page.
How can i resolve this problem using below code ?
ctrl.js
function deleteInventory(inventory,controlId) {
prcDeleteFactory.deletePrcInventory(inventory,controlId).then(function(){
$scope.confirmationWin.close();
$state.go('app.search');
});
}
factory.js
angular.module('App').factory('prcDeleteFactory',function($http){
'use strict';
return {
deletePrcInventory: function(inventoryToDelete,key){
return $http.get('app/delete/deleteInventory?inventoryToDelete=' + inventoryToDelete + '&key=' + key);
}
};
});
Upvotes: 1
Views: 99
Reputation: 1574
The second parameter for .then()
is the function that executes on error. Change your controller code to:
function deleteInventory(inventory,controlId) {
prcDeleteFactory.deletePrcInventory(inventory,controlId).then(function(){
$scope.confirmationWin.close();
$state.go('app.search');
}, function(error){
//Some error occurred here
});
}
Upvotes: 0
Reputation: 2801
What about using the then / catch form :
$http.get(/**/).then(function(response) {
// do something with response
}).catch(function(err) {
// do something with error err
})
Upvotes: 0
Reputation: 193261
You need to "catch" error. If it happens promised returned by $http service gets rejected so you can handle it with catch (or error callback in then
):
function deleteInventory(inventory,controlId) {
prcDeleteFactory.deletePrcInventory(inventory, controlId).then(function() {
$scope.confirmationWin.close();
$state.go('app.search');
})
.catch(function(error) {
$scope.error = 'Delete failed.';
});
}
Upvotes: 2