Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  67] [ 3]  / answers: 1 / hits: 24034  / 12 Years ago, sun, july 22, 2012, 12:00:00

Does anyone know how you can check to see that a resource failed to be fetched in AngularJS?



For example:



//this is valid syntax
$scope.word = Word.get({ id : $routeParams.id },function() {
//this is valid, but won't be fired if the HTTP response is 404 or any other http-error code
});

//this is something along the lines of what I want to have
//(NOTE THAT THIS IS INVALID AND DOESN'T EXIST)
$scope.word = Word.get({ id : $routeParams.id },{
success : function() {
//good
},
failure : function() {
//404 or bad
}
});


Any ideas?


More From » json

 Answers
6

An additional callback function after your first callback function should fire when there is an error. Taken from the docs and group post:



$scope.word = Word.get({ id : $routeParams.id }, function() {
//good code
}, function(response) {
//404 or bad
if(response.status === 404) {
}
});




  • HTTP GET class actions: Resource.action([parameters], [success], [error])

  • non-GET class actions: Resource.action([parameters], postData, [success], [error])

  • non-GET instance actions: instance.$action([parameters], [success], [error])



[#84102] Friday, July 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
coleman

Total Points: 518
Total Questions: 81
Total Answers: 96

Location: Aland Islands
Member since Wed, Nov 17, 2021
3 Years ago
;