Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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,我正在使用UI路由器,并试图找出如何允许用户仅从其他两个状态访问一个状态。否则,应将用户重定向到主状态 在本例中,我只希望用户能够从payment.confirmation状态和selection状态访问payment.details状态 我不知道如何将以下两条规则结合起来: $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState) { if (fromStat

我正在使用UI路由器,并试图找出如何允许用户仅从其他两个状态访问一个状态。否则,应将用户重定向到主状态

在本例中,我只希望用户能够从payment.confirmation状态和selection状态访问payment.details状态

我不知道如何将以下两条规则结合起来:

$rootScope.$on('$stateChangeStart',
      function (event, toState, toParams, fromState) {
        if (fromState.name !== 'selection' && toState.name === 'payment.details') {
          event.preventDefault();
          $state.go('home');
        }

        if (fromState.name !== 'payment.confirmation' && toState.name === 'payment.details') {
          event.preventDefault();
          $state.go('home');
        }
});
以下是各州的相关路由器信息:

.state('payment', {
      abstract: true,
      url: '/payment',
      templateUrl: 'app/views/payment.html'
})

.state('payment.details', {
    url: '/details',

    views: {
     'main': {
       templateUrl: 'app/views/cc-payment.html',
       controller: 'PaymentFormCtrl'
     }
   }
 })

.state('payment.confirm', {
      url: '/confirm',

      views: {
       'main': {
         templateUrl: 'app/views/cc-confirm.html',
         controller: 'PaymentFormCtrl'
       }
     }
   }
 })
试试这个

试试这个

$rootScope.$on('$stateChangeStart',
      function (event, toState, toParams, fromState) {
        if ((fromState.name == 'selection' || fromState.name == 'payment.confirmation ) && toState.name === 'payment.details') {
          event.preventDefault();
          $state.go('payment.details');
        }else{
          event.preventDefault();
          $state.go('home');
        }
});