2
我正在做一些ngResource上的原始(預發送)和新的(後發送)數據緩存。我爲response
和responseError
使用$resource
攔截器。如何使用responseError攔截器獲取ngResource中的原始資源?
這是問題:在response
,參數有一個屬性resource
,它允許我在將資源傳遞迴調用方之前操作資源。
在responseError
,有沒有這樣的屬性,所以我怎麼操作資源?
代碼示例:
update: { method: 'put', isArray: false, interceptor: {
response: function (response) {
// clear my pristine cache
// I have access to response.resource
angular.copy(pristineData,response.resource);
return(response);
},
responseError: function (response) {
// the PUT failed, I want to reset the data
// need to reset the data to pristine
// "pristineData" is cached elsewhere
// HOW DO I DO THIS, SINCE response.resource UNAVAILABLE?
angular.extend(response.resource,pristineData);
}
}},
我不確定但是,您可以直接嘗試訪問定義responseError中update方法的資源變量。 – Chandermani
@chandermani,你失去了我? – deitch
var r = $ resource(「url」,{id:'@id'},{update:{method:'PUT',攔截器:{responseError:function(response){//嘗試直接訪問r。 }}}})' – Chandermani