0

In a multi step form, I have a previous, next directive that gets the current step and saves it in session storage variable. All of the information on step 1 template that the user fills out is stored in a $scope.section1 object. I want to keep saving "section2", "section3" on clicking next on each of the step. how do I generate the $scope.section1, $scope.section2 dynamically? I want to preserve the notation "section1", "section2" etc.

$sessionStorage[currindex] = $scope.section1;  

Controller:

$scope.nextPage = function(){
           var currindex = $scope.getIndex();
          var nextindex = ++currindex;
         //storing in session    
        $sessionStorage[currindex] = $scope.section1;   
    }

Directive:

<nav>
    <ul class="pager">
        <li class="previous" ng-click="prevPage()" ng-class="{disabled: prevPageDisabled == true}"><a href><span aria-hidden="true">&larr;</span>Previous</a></li>
        <li class="btn btn-lg" ng-click="saveAll()"><a href><span aria-hidden="true">Save</span></a></li>
        <li class="next" ng-click="nextPage()" ng-class="{disabled: nextPageDisabled == true}"><a href><span aria-hidden="true">&rarr;</span>Next </a></li>
    </ul>
</nav>
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
neelmeg
  • 2,459
  • 6
  • 34
  • 46
  • [Dynamic variables names](http://stackoverflow.com/questions/13291554/dynamic-variables-names-in-javascript) can be an hint? – T30 Feb 23 '16 at 02:17

1 Answers1

1

use [] instead of . something like

$scope.nextPage = function(){
       var currindex = $scope.getIndex();
      var nextindex = ++currindex;
     //storing in session    
    $sessionStorage[currindex] = $scope['section' + currindex];   
}

Is that what you are looking for?

Louie Almeda
  • 5,366
  • 30
  • 38