我在routes.js中有以下状态:
.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
。我怎样才能做到这一点?
答案 0 :(得分:1)
您可以在GlobalController
或body
标记中添加一个控制器html
,以便其范围在整个网页中可用(尝试阻止使用$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
});
}]);