AngularJS失败的资源GET

mat*_*sko 29 javascript json angularjs

有谁知道如何检查AngularJS中是否找不到资源?

例如:

//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
    }
});
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Glo*_*opy 49

在出现错误时,应在第一个回调函数后触发另一个回调函数.摘自文档和小组帖子:

$scope.word = Word.get({ id : $routeParams.id }, function() {
    //good code
}, function(response) {
    //404 or bad
    if(response.status === 404) {
    }
});
Run Code Online (Sandbox Code Playgroud)
  • HTTP GET"类"操作:Resource.action([parameters],[success],[error])
  • 非GET"类"操作:Resource.action([parameters],postData,[success],[error])
  • 非GET实例操作:实例.$ action([参数],[成功],[错误])


Rod*_*ong 5

只是回答@Adio的问题.

当任何http响应代码被AngularJS视为错误时,将调用第二个回调(仅[200,300]中的响应代码被视为成功代码).因此,您可以拥有一般错误处理功能,而不关心特定错误.那里的if语句可以用来根据错误代码执行不同的操作,但它不是强制性的.