我阻止用户到另一个页面做类似的事情,但是,当到了我最初阻止的页面的时候,我会得到错误的"transition prevented”。
如何删除原始预防并允许页面继续进行?我试图返回false而不是event.preventDefault,但是它没有工作。有什么帮助吗?
$rootScope.$on($stateChangeStart, function(e, toState, toParams, fromState, fromParams) {
    if (isCurrentPageCompleted === false) {
        e.preventDefault();
    } 
});发布于 2015-02-27 07:19:38
优先逼近
如果我们希望允许用户转到某个页面,只有在加载了一些数据时,我们才可以使用resolve
.state("prevented", {
    ...
    resolve: {
        someKey : { // promise, which once resolved 
                    // - then it will allow state transition                   
                    // function() { return ... }
                  }
    }
}第二次逼近
但是,如果我们想重定向用户,直到满足某些条件,我们可以使用一些全局服务。有工作用柱塞。
.factory("BreakTransition", function(){
  return { 
    ShouldWaitGlobally : true, 
  } 
})那我们就可以有一些州。
'home')。'prevented')。'enabler')
.state('home',{ url:"/home",templateUrl:'tpl.html',}) .state('enabler',{ url:‘/enabler’,templateUrl:'tpl.enabler.html',控制器:‘EnablerController’,}) .state(‘EnablerController’,}).state(‘阻止’,{ url:‘/prevented’,templateUrl:'tpl.html',数据:{ ShouldWaitForCompletion : true,},})现在,在工厂价值改变之前,我们可以使用这个“中断”。
.run(['$rootScope', '$state', 'BreakTransition',
  function ($rootScope, $state, BreakTransition) {
  $rootScope.$on('$stateChangeStart', function(e, toState, toParams 
                                            , fromState, fromParams) {
    var shouldNotWait = toState.name === "home"
    if(shouldNotWait)
    {
      return;
    }
    var isCurrentPageCompleted = BreakTransition.ShouldWaitGlobally !== true
        || (toState.data || {}).ShouldWaitForCompletion !== true;
    if (isCurrentPageCompleted === true) {
      return;
    } 
    e.preventDefault(); 
    $state.go('home');
  });
}])检查在这里一切都在行动
https://stackoverflow.com/questions/28753499
复制相似问题