2016-07-22 50 views
1

我在routes.js以下状态:AngularJS:进入状态只有当国家为没有转变之前

.state('showorderflow', { 
    url: "#", 
    templateUrl: '/assets/views/tvx/selection.html', 
    controller: 'tvxChannelSelectCtrl' 
}) 

,在我的HTML我有以下代码:

<a class="click-to-select" href="#" 
    ng-click="packBtnClick($event)"> Click to order </a> 

和我的控制器看起来像这样:

$scope.packBtnClick = function ($e) { 
    $state.go('showorderflow'); 
    // do something else. 
} 

我想去状态showorderflow只有当它一直没有transiti在之前。我怎样才能做到这一点?

+0

设置一个标志,在rootScope或一旦加载'tvxChannelSelectCtrl',服务就会立即生效。然后在调用'$ state.go('showorderflow');' – matmo

回答

1

您可以添加一个控制器说GlobalControllerbodyhtml标记,以便其范围是在整个页面中提供(试图阻止使用$rootScope)。现在,在GlobalController,你可以添加一个侦听状态变化:

$scope.$on('$stateChangeStart', function(e, toState) { 
    if (toState.name === 'showorderflow') { 
     // Use a key "hasBeenTransitionedBeforeFoo" to check if the user is already transitioned to that state before 
     if (toState.hasBeenTransitionedBeforeFoo) { 
      // If yes, then prevent the navigation 
      e.preventDefault(); 
      return; 
     } 

     // Otherwise mark it as true on first time transition 
     toState.hasBeenTransitionedBeforeFoo = true; 
    } 
}); 

或者,你可以注册在你的应用程序的run块相同的回调

myApp.run(['$rootScope', function($rootScope) { 
    $rootScope.$on('$stateChangeStart', function(e, toState) { 
     // same code goes here 
    }); 
}]); 
+0

ahh cool之前检查该标志。好戏!谢谢。 –

相关问题