2016-10-25 9 views
0

ユーザーがログインしていない場合は、状態の変更を防止したい。私はコードの下で使用して、うまく動作します。角度が状態を変更するのを防ぐユーザーがログインしない

angular.module('app', [...]) 
     .config(function(){}) 
     .run(function($rootscope,$auth,$state){ 

     $rootScope.$on("$stateChangeStart", function(event){ 
     var user = $auth.getToken(); 
     if (user === null){ 
      // User isn’t authenticated 
      $state.transitionTo("index"); 
      event.preventDefault(); 
     } 
     }); 

     }) 

が、このエラーを表示!

angular.js:12783 RangeError: Maximum call stack size exceeded 
at Array.indexOf (native) 
at indexOf (http://localhost:9000/bower_components/angular-ui-router/release/angular-ui-router.js:87:18) 
at http://localhost:9000/bower_components/angular-ui-router/release/angular-ui-router.js:1708:46 
at forEach (http://localhost:9000/bower_components/angular/angular.js:341:20) 
at http://localhost:9000/bower_components/angular-ui-router/release/angular-ui-router.js:1707:9 
at forEach (http://localhost:9000/bower_components/angular/angular.js:341:20) 
at Object.$$keys (http://localhost:9000/bower_components/angular-ui-router/release/angular-ui-router.js:1706:7) 
at Object.$$validate [as $$validates] (http://localhost:9000/bower_components/angular-ui-router/release/angular-ui-router.js:1729:23) 
at Object.transitionTo (http://localhost:9000/bower_components/angular-ui-router/release/angular-ui-router.js:3184:27) 
at http://localhost:9000/scripts/app.js:114:24 

どれでもお手伝いできますか?

答えて

0

お試しください。私に知らせてください。

angular.module('app', [...]) 
     .config(function(){}) 
     .run(function($rootscope,$auth,$state){ 

     $rootScope.$on("$stateChangeStart", function(event){ 
     var user = $auth.getToken(); 
     if (user === undefined){ 
      // User isn’t authenticated 
      $state.transitionTo("index"); 
      event.preventDefault(); 
     } 
     }); 

     }) 
1

あなたstateChangeStart機能は、ループを作成し、あなたがエラーを取得している理由です。

  1. 状態変化が
  2. ユーザーが
  3. 状態を認証されていない始まり「インデックス」
  4. 状態変化が
  5. ユーザーが「インデックス」に行く
  6. 状態を認証されていない起動しに行く:これを考えてみましょう

認証用の状態の変数を試すこともできますたとえば、

$rootScope.$on("$stateChangeStart", function(event, toState){ 
    if (toState.auth) { 
     var user = $auth.getToken(); 

     if (!user) { 
     // User isn’t authenticated 
     event.preventDefault(); 
     $state.transitionTo("index"); 
    } 
    } 
    }); 
+0

常にtrueを返しますか?!!!!! –

関連する問題