카테고리 없음

[자바 스크립트] 목적지 상태 또는 부모가 promise로 해결 될 때 $ state.go가 작동하지 않는 이유

행복을전해요 2021. 2. 7. 02:01

그리고 내 자신의 질문에 대답하기 위해-$ state.go ( 'home.other')가 호출 된 후 요청 된 URL을 처리하기 시작하고 resolve 함수가 deffered 개체를 생성함에 따라 다이제스트 주기로 인해 원하지 않는 동작이 발생했지만이 개체가 해결되었지만 상태 엔진 이미 요청 된 URL의 상태로 전달됩니다. 그래서 이것을 방지하기 위해 아래 설명 된 기술을 사용했습니다.

응용 프로그램이 시작될 때 특정 커큐 스턴스에서 요청 된 URL의 상태 확인 실행을 취소해야하는 경우 다음과 같이 $ stateChangeStart 이벤트를 사용할 수 있습니다.

app.run(['$state', '$rootScope', '$timeout', function ($state, $rootScope, $timeout) {
  var appStarted = 0; // flag to redirect only once when app is started
    $rootScope.$on('$stateChangeStart', 
      function(event, toState, toParams, fromState, fromParams) { 
          if(appStarted) return;
              appStarted = 1;   
                  event.preventDefault(); //prevents from resolving requested url
                      $state.go('home.other'); //redirects to 'home.other' state url
                        });  
                        }]);
                        
-------------------

user3357257제공 하는 동일한 링크 의 대체 솔루션도 있습니다 .

app.run(['$state', '$rootScope', '$timeout', function ($state, $rootScope, $timeout) {
  $timeout(function() { $state.go('home.other'); });   
  }]);
  

트릭은 래핑하는 것입니다 $state.go()으로 $timeout()는 오버라이드 (override)하지 않도록.



출처
https://stackoverflow.com/questions/22050215