2014-09-24 81 views
0

我已经配置这样的$stateProvider进入角UI路由器的所有状态编程

angular.module('example', ['ui.router']) 
 
    .config(function($stateProvider, $urlRouterProvider) { 
 
    $urlRouterProvider.otherwise("/steps"); 
 
    $stateProvider.state('steps', { 
 
     url: '/steps', 
 
     templateUrl: 'steps.html', 
 
     controller: function($scope, $state) { 
 
      $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { 
 
      $scope.currentStep = toState.data && toState.data.stepNumber; 
 
      }); 
 
      if ($state.is('steps')) { 
 
      $state.go('.first'); 
 
      } 
 
     } 
 
     }) 
 
     .state('steps.first', { 
 
     url: '/first', 
 
     template: 'First', 
 
     data: { 
 
      stepNumber: 0 
 
     }, 
 
     }) 
 
     .state('steps.second', { 
 
     url: '/second', 
 
     template: 'Second', 
 
     data: { 
 
      stepNumber: 1 
 
     }, 
 
     }) 
 
     .state('steps.third', { 
 
     url: '/third', 
 
     template: 'third', 
 
     data: { 
 
      stepNumber: 2 
 
     }, 
 
     }); 
 
    }); 
 
angular.element(document).ready(function() { 
 
    angular.bootstrap(document, ['example']); 
 
});
ul { list-style: none; } 
 
li { display: inline-block; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.min.js"></script> 
 
<script type="text/ng-template" id="steps.html"> 
 
    <h2>{{ currentStep }}</h2> 
 
    <div ui-view></div> 
 
    <button ng-if="currentStep !== 0">Prev</button> 
 
    <button>Next</button> 
 
</script> 
 
<div> 
 
    <h1>Example</h1> 
 
    <ul> 
 
    <li><a ui-sref="steps.first">goTo First</a> 
 
    </li> 
 
    <li><a ui-sref="steps.second">goTo Second</a> 
 
    </li> 
 
    <li><a ui-sref="steps.third">goTo Third</a> 
 
    </li> 
 
    </ul> 
 
    <div ui-view></div> 
 
</div>

我想现在要做的,是让按钮自动指向下一步/上一步,结束下一步按钮应该消失,如果到达最后一步。可悲的是这个例子似乎并没有被工作的堆栈溢出,所以我还创建了一个jsfiddle版本这个例子

UPDATE

澄清:我想要的东西的动态,这样我就不必改变代码如果步骤顺序发生变化,步骤将被删除,或添加新步骤。

+0

'<按钮NG-如果=“currentStep!== 2”>下一个' – Donal 2014-09-24 15:25:44

+0

http://jsfiddle.net/gjcg596b/1/ – Donal 2014-09-24 15:26:26

+0

我希望它是动态的,这不会工作id我添加步骤,并且按钮仍然不会工作 – jigfox 2014-09-24 15:29:56

回答

0

它不是很干净,但您可以根据当前步骤生成状态名称。

链接功能changeStep(),将采取增量在PARAMS和做这样的事情

$scope.changeStep = function (delta) { 
     var newStep = $scope.currentStep + delta; 
     var stateName = "steps."; 

     switch (newStep){ 
      case 1: 
       stateName += "first"; 
       break; 
      etc... 
     } 

     $state.go(stateName); 
} 

所以在你的模板steps.html做到这一点

<button ng-if="currentStep !== 0" ng-click="changeStep(-1)">Prev</button> 
    <button ng-click="changeStep(1)" ng-if="currentStep !== 2">Next</button> 
+0

这会工作,但它不像我想要的那样动态,我想要butto ns独立于步骤,以便我可以更改步骤,但不必更改按钮。 – jigfox 2014-09-24 17:26:37

+0

为了避免开关的唯一的解决方案是改变你的步骤名称,并使用“step.1”,所以更改步骤功能将是'$ state.go(stateName + newStep)' – 2014-09-25 07:46:08

+0

但是,我将如何检测最后一步与动态创建的步骤 – jigfox 2014-09-26 17:19:14