AngularJS失败的资源GET [英] AngularJS Failed Resource GET

查看:182
本文介绍了AngularJS失败的资源GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何检查地看到,资源未能AngularJS要获取?

例如:

  //这是有效的语法
$ scope.word = Word.get({ID:$ routeParams.id},函数(){
    //这是有效的,但是,如果HTTP响应是404或任何其他的http错误code不会被解雇
});//这是一起什么,我希望有东西线
//(注意,这是无效的,不存在)
$ scope.word = Word.get({ID:$ routeParams.id},{
    成功:函数(){
      //好
    },
    故障:功能(){
      // 404或坏
    }
});

任何想法?


解决方案

当有错误你的第一个回调函数后,另外一个回调函数应该解雇。从文档和组的帖子

  $ scope.word = Word.get({ID:$ routeParams.id},函数(){
    //好的code
},函数(响应){
    // 404或坏
    如果(response.status === 404){
    }
});


  

      
  • HTTP GET下课的行动:Resource.action([参数],[成功],[错误])

  •   
  • 非GET下课的行动:Resource.action([参数],POSTDATA,[成功],[错误])

  •   
  • 非获得实例动作:例如$行动([参数],[成功],[错误])

  •   

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?

解决方案

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])

这篇关于AngularJS失败的资源GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆