如何转到Angular.js中的下一个或上一个步骤?

时间:2016-07-25 08:58:04

标签: javascript jquery angularjs

我正在使用ui-route进行路由。有多种顺序形式。 当我提交表格后,我想进入下一步表格(步骤)。 我不想在$ state.go()函数中硬编码步骤名称。 有没有办法获得下一个和以前的状态?

  $stateProvider
               .state("Cars", {
                  url: "/",
                  templateUrl: "/rc1/renderStep/cars",
               })
               .state("Drivers", {
                  url: "/drivers",
                  templateUrl: "/rc1/renderStep/drivers",
                  controller: "renderStepCtrl",
               })
        }])

状态变更电话

$scope.onSubmit = function () {
                  postDtoFactory.postDto(); 
                  console.info('current page= '+$state.current.name);
                  //console.log('prev = '+$state.previous.route.name);
                  $state.go();
               };

1 个答案:

答案 0 :(得分:1)

  

通过创建一个监听器

手动保存以前的状态
 $rootScope.$on('$stateChangeSuccess', function(event, to, toParams, from, fromParams) {
        //save the previous state in a rootScope variable so that it's accessible from everywhere
        $rootScope.previousState = from;
    });
  }]);
$scope.onSubmit = function(){
  //if u want to go on previous state
  $state.go($rootScope.previousState.name)
}