2017-01-12 6 views
2

トークンが解決されない場合、すべての$ http要求を角で防止する方法を教えてください。 トークンが、私はあなたがすべての要求をインターセプトし、トークンが

angular.module('myModule', []) 
.config(function($httpProvider) { 
    $httpProvider.interceptors.push(function($q) { 
     var canceler = $q.defer(); 
     return { 
      'request': function(config) { 
       if(!window.localStorage.getItem('token')) { 
       config.timeout = canceler.promise; //cancelled       
       } 
      } 
     } 
    }); 
}) 
+0

$ httpラッパー/プロキシを作成し、ポスト、ゲットなどのオーバーライドにチェックを入れてください。 – Igor

答えて

0

のようなものを探していますのlocalStorage

にログイン時に保存されています開始前でもリクエストを拒否する

angular 
    .module('app', []) 
    .config(function ($httpProvider) { 
     $httpProvider.interceptors.push(function($q) { 
      return { 
       'request': function(config) { 
        if(!localStorage.getItem('token')) { 
         return $q.reject() 
        } else { 
         return config 
        } 
       } 
      } 

     }) 
    }) 
0

することができますが設定されていない場合は、それらをCANCELLでき

app.config("$httpInterceptor", function(){ 
    var token = localStorage.getItem("token") 
    if (token == null){ 
//prevent all $http calls (get, post, put whatever calls) 
} 
}) 
0

インターセプタの概念を適用する。

app.factory('httpCanceller', ['$q', function($q) { 
    return { 
    'request': function(config) { 

     var canceler = $q.defer(); 
     var token = localStorage.getItem("token"); 

     config.timeout = canceler.promise; 

     if (!token) { 
      canceler.resolve(); 
     } 

     return config; 
    } 
    } 
}]); 


app.config('$httpProvider', function($httpProvider){ 
    httpProvider.interceptors.push('httpCanceller'); 
}); 
関連する問題