簡體   English   中英

為什么$ state.go在目標狀態或其父級以承諾解析時不起作用

[英]Why $state.go doesn't work when destination state or it's parent resolves with promise

我嘗試使用resolve加載父狀態的一些數據,並在應用程序運行時將用戶重定向到默認狀態:

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {

  $stateProvider.state('home', {
    url: '/',
    template: '<div><a ui-sref="home">Start App</a> <a ui-sref="home.main">Home</a> <a ui-sref="home.other">Other state</a><div ui-view>Loading...</div></div>',
    resolve: {
      user: ['$timeout', '$q', 
        function($timeout, $q) {
          var deferred = $q.defer();
          var promise = deferred.promise;
          var resolvedVal = promise.then(function() {
            console.log('$timeout executed');
            return {Id: 12, email: 'some@email.com', name: 'some name'};
          }, function() {
            console.log('Error happend');
          });
          $timeout(function() {
            deferred.resolve();
          }, 2000);
          return resolvedVal;
        }]
      //user: function() {return {id: 222, name: 'testname', email: 'test@em.ail'}; }
    },
    controller: ['$scope', 'user', function($scope, user) {
      $scope.user = user;
    }]
  });

  $stateProvider.state('home.other', {
    url: 'other',
    template: '<div>Your name is {{user.name}}, and email is {{user.email}}</div><div>This is other state of application, I try to make it open as default when application starts, by calling to $state.transitionTo() function in app.run() method</div>',
    resolve: {
      someObj: function() {
        console.log('hello');
        return {someProp: 'someValue'};
      }  
    },
    controller: ['$scope', 'user', function($scope, user) {
      $scope.user = user;
    }]    
  });

}]);  

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

並且這不會更改地址欄中的url並且不顯示home.other狀態的模板(雖然home.state的解析功能已執行且控制台中有'hello')。 但是當我在解析中評論promise函數時,反而把簡單的函數返回對象應用程序重定向到預期。

而不是$ timeout嘗試做$ http請求實際上會在那里,但也沒有運氣。

並回答我自己的問題 - 由於在$ 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()這樣它就不會被覆蓋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM