2017-12-11 13 views
1

状態変更が発生したときに保留中の要求をすべて停止しようとしています。以下のコードは$ httpではうまく動作しますが、$ resourceでは正常に動作しません。

    var cancel=$q.defer(); 
    $http.get('api/cafservicestatus',{timeout:cancel.promise,cancel:cancel}).then(onsuccess,onerror); 

    function onsuccess(result) { 
     console.log(result) 
    } 

    function onerror(error) { 
     console.log(error) 
    } 


    function stopHttp() { 
     $http.pendingRequests.forEach(function(request) { 
      if (request.cancel) { 
       request.cancel.resolve(); 
      } 
     }); 
    } 

しかし、このコードはここで$リソース では動作しません$

   $resource(resourceUrl, {}, { 
     'query': { method: 'GET', isArray: true,timeout:$q.defer().promise,cancel:$q.defer()}, 
     'get': { 
      method: 'GET', 
      transformResponse: function (data) { 
       if (data) { 
        data = angular.fromJson(data); 
       } 
       return data; 
      } 
     }, 
     'update': { method:'PUT' } 
    }); 

私は考え出した$リソース

答えて

1

を使用してすべての要求をどのように停止することができますが、リソースのサンプル・コードでありますcancelable:あなたの要求にtrueを加え、$ resourceの$ cancelRequest()メソッドを使用してコントローラ内の要求を取り消す必要があります。例えば

、あなたの場合は

$resource(resourceUrl, {}, { 
    'query': { method: 'GET', isArray: true,cancellable:true}, 
    'get': { 
     method: 'GET', 
     transformResponse: function (data) { 
      if (data) { 
       data = angular.fromJson(data); 
      } 
      return data; 
     } 
    }, 
    'update': { method:'PUT' } 
}); 

とあなたのコントローラ内

var aborted= YourService.query(); 

//and later on state change 

    $scope.$on('$stateChangeSuccess',function(event){ 
    aborted.$cancelRequest(); 
     } 
    ); 
+0

これが唯一の私がしたい特定のcontroller.Whatのための問題を解決には、一度、$ rootScope上ですべての要求をキャンセルすることですしかし、とにかく、ありがとう –

関連する問題