您可以收听'$stateChangeStart'
event并致电event.preventDefault()
以防止用户实际转到新视图。
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){
event.preventDefault();
// transitionTo() promise will be rejected with
// a 'transition prevented' error
});
注意,如果要添加控制器里面这个监听器,你应该保持在注销函数的引用,以便在你的范围被破坏,除去监听器。
var removeListenerFn = $rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){
event.preventDefault();
// transitionTo() promise will be rejected with
// a 'transition prevented' error
}
);
$scope.$on('$destroy',function(){
removeListenerFn();
});
下面是一个简单Plunker你可以用它来测试一下:http://plnkr.co/edit/lYfkPuePGv1GlN1sFjW4?p=preview
https://github.com/angular-ui/ui-router/wiki#state-change-events – JoseM 2014-10-10 16:08:20