2014-02-21 55 views
0

在角每個資源的響應,我想有一個攔截$資源響應它到達控制器之前,看它是否有一個錯誤標誌設置,然後對錯誤標誌行爲的功能。是否有一個資源函數可以在響應數據發送到控制器之前檢查響應數據?角檢查錯誤標誌

樣本資源:

mymod.factory('setSomething', function($resource){ 
    var resource = $resource('/proxy/request.php?action=setSomething', {}, { 
     post:{ 
      method : "POST", 
      isArray : false, 
      headers : { 
       'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8' 
      } 
     }, 

    }); 
    return resource; 
}); 
+0

入住這對方的回答,應該給它正是你需要的。 http://stackoverflow.com/questions/18582900/usage-of-interceptor-within-resource – pedromarce

+0

你或許可以使用的$ HTTP攔截。有[這裏]好的帖子(http://djds4rce.wordpress.com/2013/08/13/understanding-angular-http-interceptors/)關於攔截。 – maxdec

回答

0

您可以定義錯誤處理資源全球:

$httpProvider.interceptors.push(function($q, $rootScope) { 
    return { 
    'request': function(config) { 
     // intercepts the request 
    }, 
    'response': function(response) { 
     // intercepts the response. you can examine things like status codes 
    }, 
    'responseError': function(response) { 
     // intercepts the response when the response was an error 
    } 
    } 
}); 

的響應參數爲您提供了大量的信息,以使您可以適當地處理錯誤的工作。