2016-11-04 209 views
0

我使用的是angularjs 1.5.8。

我得到這個錯誤,當我試圖取消HTTP請求與角:

$cancelRequest is not a function

我的代碼:

app.factory('User', function($resource) { 
    var getUsersResource = $resource(
     '/users', 
     null, 
     {get : {method: 'GET', isArray: true, cancellable: true}} 
    ); 

    return { 
     getUsers : function() { 
      return getUsersResource.get({}, 
       function(data) { 
        ... 
       }, function(error) { 
        ... 
       } 
      ); 
     } 
    }; 
}); 

app.controller('InitController', function($rootScope, User, ...) { 
    ... 
    User.getUsers(); 
    ... 
} 

app.factory('AuthInterceptor', function($q, $location, $injector) { 
    return { 
     responseError: function(response) { 
      if (response.status === 401) { 
       $injector.get('$http').pendingRequests.forEach(
        function (pendingReq) { 
         pendingReq.$cancelRequest(); 
        } 
       ); 
       $location.path('login'); 
      } 
      return $q.reject(response); 
     } 
    }; 
}); 

你知不知道我怎樣才能解決這個問題?

感謝

回答

0

的文件表明,$cancelRequest應與資源對象使用。從我的初步審查來看,您在User工廠內正確使用$資源。但是,我不確定你是如何在AuthInterceptor工廠內實施這個的。它看起來並不像你使用User.getUsersSources()。因此,我相信你得到這個錯誤的原因是因爲你沒有正確使用$cancelRequestion。話雖如此,你可能忘記了包含代碼的其他部分。

理想情況下,解析的$resource對象User.getUserResources()應傳遞到AuthInteceptor

+0

當我做User.getUsersResource()我得到沒有$ cancelRequestion方法 '數組[0] $承諾此對象:d $解析:假 長度:0 __proto__:數組[0]' – Nan

+0

燦你舉一個你的解決方案的例子嗎? – Nan

+0

我沒有一個例子。簡單地去掉我從文檔中讀到的內容:https://docs.angularjs.org/api/ngResource/service/$resource – richdotjs