Angular-ui-routing,有条件地阻止所有路由

时间:2015-01-28 12:51:50

标签: angularjs angular-ui-router

这是我的角度应用程序的运行块。 当用户没有设置他的帐户,即用户没有在帐户中配置他的货币和时区时,我希望将用户重定向到AccountSetup页面,以及要阻止的所有其他路由。

这总是有效的,除非用户在第一次交互时直接在worng url处开始,就像用户打开一个新标签并直接放入网址“www.host.com/channel1”一样,它会遇到无限的消化循环,在10个循环后终止。 我不知道这里有什么问题,它一直在状态channel1State和accountSetup状态之间切换。

.run(["$stateParams","$rootScope","$state","$location", function($stateParams,$rootScope,$state,$location) {
$rootScope.$state = $state;
$rootScope.stateParams = $stateParams;
$rootScope.plan = myPlan;
// Edit below line when adding more channels
$rootScope.channels = myPlan.channel1 || myPlan.channel2;
$rootScope.timezone = timezone;
$rootScope.currency_type = currency_type
// this gets triggered whenever a user is not authorized
// to visit a url through out the application.
$rootScope.$on('$stateChangeError', function(event){
  $state.go('defaultState');
})
// this gets triggered on reload
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
  // captures the state for the first reload
  debugger
  console.log("Moving to: ", toState.name)
  if (toState.name === 'rootpath'){
    event.preventDefault();
    if($rootScope.timezone && $rootScope.currency_type){
      if(myPlan.channel1)
        $state.go('channel1State');  // redirect to campaign path
      else
        $state.go('channel2State');  // redirect to segment path
    }
    else{
      $state.go('accountSetup'); //redirect to account setup page
    }
  }
  else if((!$rootScope.timezone || !$rootScope.currency_type) && toState.name != 'accountSetup'){
        event.preventDefault();
        $state.go('accountSetup'); //redirect to account setup page
    }
})
}])

1 个答案:

答案 0 :(得分:0)

这段代码有点难读,但它看起来像是最后一个 - 如果可能会阻止它。

  if (toState.name === 'rootpath'){
            ...
  }
  else if((!$rootScope.timezone || !$rootScope.currency_type) && toState.name != 'accountSetup'){
    event.preventDefault();
    $state.go('accountSetup'); //redirect to account setup page
  }

当它重定向到状态'accountSetup'时,我认为没有一个IF语句可以让它进入。自'accountSetup'!=='rootPath'它将绕过第一个IF块。然后第二个只在toState.name!='accountSetup'时运行。当它是==='accountSetup'时,它被忽略。

我没有尝试过这段代码,但它可能更接近你实际想要实现的目标:

.run(["$stateParams","$rootScope","$state","$location", function($stateParams,$rootScope,$state,$location) {
$rootScope.$state = $state;
$rootScope.stateParams = $stateParams;
$rootScope.plan = myPlan;
// Edit below line when adding more channels
$rootScope.channels = myPlan.channel1 || myPlan.channel2;
$rootScope.timezone = timezone;
$rootScope.currency_type = currency_type
// this gets triggered whenever a user is not authorized
// to visit a url through out the application.
$rootScope.$on('$stateChangeError', function(event){
  $state.go('defaultState');
})
// this gets triggered on reload
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
  // captures the state for the first reload
  debugger
  console.log("Moving to: ", toState.name)
  if (toState.name === 'accountSetup' || !$rootScope.timezone || !$rootScope.currency_type) {
        event.preventDefault();
        $state.go('accountSetup'); //redirect to account setup page
  } else if (toState.name === 'rootpath') {
    event.preventDefault();
      if(myPlan.channel1) {
        $state.go('channel1State');  // redirect to campaign path
      } else {
        $state.go('channel2State');  // redirect to segment path
      }
  }
})
}])