2016-10-25 4 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를 반환합니까? !!!!! –