在$在('$ stateChangeStart')UI-路由器$ state.go是cauzing一个无限循环 [英] Ui-Router $state.go inside $on('$stateChangeStart') is cauzing an infinite loop

查看:542
本文介绍了在$在('$ stateChangeStart')UI-路由器$ state.go是cauzing一个无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图引进登录到用户导航翻过的应用程序的方式。

我pretend给用户的页面重定向是他之前,他浏览到登录页面,如果该页面满足特定要求

preventing从$ stateChangeStart停事件的状态更改像预期的,但是当我运行$ state.go('into_somewhere')我进入无穷远环

我的角度版本是1.3.1和UI路由器是最新的

  .factory('RouteHistory',函数($ rootScope,$日志$状态,验证,$ urlRouter,$超时){    //用户后,输入页面
    VAR currentState的='';    //当用户试图访问他没有权限的网页
    //或者任何需要先登录用户
    VAR pendingState ='';    VAR isMenuTogglerVisible = FALSE;
    VAR skipFromStateVal =真;    $ rootScope。在$('$ stateChangeStart',函数(事件,toState,toParams,fromState,fromParams){      。事件preventDefault();      如果(toState.name =='登录'和;&安培;!fromState.name ='登录'){
        $ log.log(UI路由器:更改登录');
        // $ urlRouter.sync();
        $ state.go('登录')
        // pendingState = fromState;
        //$log.log('Peding状态更新为:'+ pendingState.name);
        // $ urlRouter.sync();
      }      如果(fromState.name =='登录'和;&安培; Auth.isLoggedIn()){
        $ log.log(UI-路由器:登录去');
        //$state.go(fromState.name);
        $超时(函数(){
          // $ state.go(家,空,{/ *重载:真的,地点:'取代'* /});
          $ state.go('浏览机');
          // $ urlRouter.sync();
        },2000)
      }      $ log.log({
        toState':toState,
        toParams':toParams,
        fromState':fromState,
        fromParams':fromParams
      })    })
    返回{    };
  });


解决方案

在一般我会说,让我们重新定向的 $ state.go() 仅在需要。在其他情况下,从事件监听出去:

 如果(toState.name ==='登录'){
  //母鹿她/他尝试去登录? - 让他/她去
  返回;
}如果(Auth.isLoggedIn()){
   //已经登录? - 可以去anyhwere
   返回;
}//其他
$ state.go('登录')

这是简化的逻辑,但表明,我们应该改变只在需要执行。还有一些其他的examles更详细的实施和plunkers:

由于在评论规定,有 plunker ,这是我改变了这样的这里

  ...
//三个新线
如果(toState.name ==='specialRoute'){
  返回;
}如果(fromState.name =='ROUTE1'){
  。事件preventDefault();
  $ state.go('specialRoute')
}

这是不会循环了。请检查这里

I'm trying to introduce login into the way the user navigates accross the application.

I pretend to redirect the user to the page were he was before he navigate to the login page if that page meets specific requirements

Preventing the event from the $stateChangeStart stop's the state change like expected but when i run the $state.go('into_somewhere') i enter an infinit loop

My angular version is 1.3.1 and the ui-router is the latest

.factory('RouteHistory', function ($rootScope,$log, $state, Auth, $urlRouter, $timeout) {

    // after the user enter a page
    var currentState = '';

    // when the user is trying to access a page that he has not permissions
    // or that requires the user to be logged in
    var pendingState = '';

    var isMenuTogglerVisible = false;
    var skipFromStateVal = true;

    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){

      event.preventDefault();



      if (toState.name == 'login' && fromState.name != 'login'){
        $log.log('Ui-router: changing to login');
        // $urlRouter.sync();
        $state.go('login')
        //pendingState = fromState;
        //$log.log('Peding state updated to:' + pendingState.name );
        //$urlRouter.sync();
      }

      if (fromState.name == 'login' && Auth.isLoggedIn()) {
        $log.log('Ui-router: going from login');
        //$state.go(fromState.name);
        $timeout(function(){
          // $state.go('home', null, {/*reload: true, location: 'replace'*/});
          $state.go('browse-machine');
          //$urlRouter.sync();
        },2000)
      }



      $log.log({
        'toState': toState,
        'toParams': toParams,
        'fromState': fromState,
        'fromParams': fromParams
      })

    })


    return {

    };
  });

解决方案

In general I would say, let's redirect ($state.go()) only if needed. In other cases, get out from the event listener:

if (toState.name === 'login' ){
  // doe she/he try to go to login? - let him/her go
  return;
}

if(Auth.isLoggedIn()){
   // is logged in? - can go anyhwere
   return;
}

// else
$state.go('login')

This is simplified logic, but shows, that we should change to execution only if needed. There are some other examles with more detailed implementation and plunkers:

As provided in the comment, there was plunker, which I changed like this here

...
// three new lines
if (toState.name === 'specialRoute'){
  return;
}

if (fromState.name=='route1'){
  event.preventDefault();
  $state.go('specialRoute')
}

And this is not looping anymore. Please, check it here

这篇关于在$在('$ stateChangeStart')UI-路由器$ state.go是cauzing一个无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆