AngularJS 错误怎么统一处理

黑牙菜菜 发布于 2014/07/14 22:27
阅读 10K+
收藏 1
直接上代码:

<script type="text/javascript">
<!--




//-->
function get($scope,$http)
{
 $scope.item={};
 $http.get("/admin/category/get").success(function(data)
 {
 $scope.data=data;
 }).error(function(data,state)
 {
 if(state==403)
 {
 //do .....
 }
 });
 $scope.dialog=function(index,item)
 {
 if(index<0)
 {	$scope.create=true;
 $scope.title="新建";
 $scope.url="/admin/category/add";
 $scope.item={};




 }else{
 $scope.create=false;
 $scope.title="编辑";
 $scope.url="/admin/category/updata";
 $scope.item=item;
 $scope.index=index;
 $scope.old=angular.copy(item);




 }
 $('#dialog').modal('show');
 };
 $scope.remove=function(index,item)
 {
 $http.post("/admin/category/remove?id="+item.id).success(function(data)
 {
 var temp=$scope.data.splice(index,1);
 
 }).error(function(data,state)
 {
 if(state==403)
 {
 //do .....
 }
 });;
 
 }




 $scope.submit=function()
 {




 $http.post($scope.url,$scope.item).success(function(data,status, headers)
 {
 
 if($scope.create)
 {
 $scope.data.push(data);
 }
 $('#dialog').modal('hide');
 });
 };
 $scope.close=function()
 {
 if($scope.index>=0){
 $scope.data[$scope.index]=$scope.old; 
 
 }
 $('#dialog').modal('hide');
 
 
 }
 
 }




</script>




这断代码如果我想统一处理Ajax response 的error 部分怎么处理,想找一个全局的处理方法


最好用var $injector = angular.injector();啥的!!!
加载中
0
aiasfina
aiasfina

这个是我两年前写的代码,那时还ng不稳定,不知道现在还能不能用....

原理就是插入一个拦截器到 responseInterceptors,代码放到 module 定义下:

angular.module('app', ['ngSanitize'],
  ['$httpProvider', '$routeProvider', '$locationProvider', ($httpProvider, $routeProvider, $locationProvider) ->
    interceptor = ['$rootScope', '$q', ($scope, $q) ->
      success = (response) ->
        response
      error = (response) ->
        status = response.status
        if status is 400
          $.jGrowl response.data.join('<br />')
        else if status is 403
          $.jGrowl response.data
        else
          $.jGrowl "错误:#{status}"
        deferred = $q.defer()
        req =
          config:response.config
          deferred:deferred
        $q.reject(response)
      (promise) ->
        promise.then(success, error)
    ]
    $httpProvider.responseInterceptors.push(interceptor)
)



hantsy
hantsy
现在 responseIntecepters 在 1.2 中已经过时了。
0
dkf_genius
dkf_genius
http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/
0
Tkks
Tkks
。。自己就不能做个service把http封装一下吗....非要复制粘贴啊
0
hantsy
hantsy



这个我是处理 401 的,先抓到 401,然后通过事件机制处理。。。

$httpProvider.interceptors.push(function($rootScope, $q) {

            return {
                'request': function(config) {
                    // console.log('request:' + config);
                    return config || $q.when(config);
                },
                'requestError': function(rejection) {
                    // console.log('requestError:' + rejection);
                    return rejection;
                },
                //success -> don't intercept
                'response': function(response) {
                    // console.log('response:' + response);
                    return  response || $q.when(response);
                },
                //error -> if 401 save the request and broadcast an event
                'responseError': function(response) {
                    console.log('responseError:' + response);
                    if (response.status === 401) {
                        var deferred = $q.defer(),
                                req = {
                                    config: response.config,
                                    deferred: deferred
                                };
                        $rootScope.requests401.push(req);
                        $rootScope.$broadcast('event:loginRequired');
                        return deferred.promise;
                    }
                    return $q.reject(response);
                }

            };
        });







返回顶部
顶部