Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 角度ui路由,有条件地阻止所有路由_Angularjs_Angular Ui Router - Fatal编程技术网

Angularjs 角度ui路由,有条件地阻止所有路由

Angularjs 角度ui路由,有条件地阻止所有路由,angularjs,angular-ui-router,Angularjs,Angular Ui Router,这是我的angular应用程序的run块。 当用户未设置其帐户,即用户未在帐户中配置其货币和时区时,我希望用户重定向到AccountSetup页面,并阻止所有其他路由 这总是有效的,除非用户在第一次交互中从worng url开始,例如,如果用户打开一个新选项卡并直接放入url“www.host.com/channel1”,它将进入无限摘要循环,该循环在10个循环后终止。 我不明白这里出了什么问题,它一直在channel1State和accountSetup状态之间切换 .run(["$state

这是我的angular应用程序的run块。 当用户未设置其帐户,即用户未在帐户中配置其货币和时区时,我希望用户重定向到AccountSetup页面,并阻止所有其他路由

这总是有效的,除非用户在第一次交互中从worng url开始,例如,如果用户打开一个新选项卡并直接放入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
    }
})
}])

这段代码有点难读,但它看起来像是上一个else if可能阻止了它

  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”时,它将被忽略

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

.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
      }
  }
})
}])

如果(toState.name=='accountSetup'){event.preventDefault()$state.go('accountSetup')}这将给您一个无限循环。我的错是,我看到“accountSetup”是要通过块的。是的,这是一个奇怪的问题,因为代码适用于除一个场景之外的所有场景。无法找出原因。能否发布状态提供程序的代码,显示路由器状态是如何设置的?当用户进入存在但不应进入的状态时,或者当用户进入不存在状态时,是否会触发问题。(www.host.com/channel1是应用程序的有效URL吗?)当用户进入存在但不应进入的状态时,会触发该问题。此外,仅当用户尝试在新选项卡中访问url时,才会触发此操作,而当用户在已打开的选项卡中单击链接时,则不会触发此操作。“www.host.com/channel1”是的,这是一个有效的url可能相关:修复:)suggets window.location作为解决方法。