在将步骤表单更改为上一步骤时丢失范围数组数据

时间:2018-03-09 06:13:58

标签: angularjs

我在最后一步中以多个步骤保存数据我使用范围内的数组数据当我在数组数据的最后一步点击前一个按钮时,数据从范围丢失。

我有一个ng-repeat显示一个数据列表,每个数据都有一个输入。我希望能够将每个输入的值与每个项目中的参数一起推送到新数组中。

     # This is my script #
     <script>   
        $scope.submitted = true;
        var arr = [];

        for(var i=0; i<$scope.choices.length; i++)  {
            arr.push({
                items: $scope.choices[i].items,
                no_of_cartoons: $scope.choices[i].no_of_cartoons,
                size: $scope.choices[i].size,
                volume: $scope.choices[i].volume
            });
            var myJSON = JSON.stringify(arr);
        }

        var res = $http({
            method: 'POST',
            url:_url+"function_ajax.php",
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj)
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            },
            data: 
            {
                surveyHomeContrl:true,
                invData: myJSON,
                last_id:$scope.last_id
            }
        });
        res.success(function(html) {
            if(html=='1')
            {   
                $scope.isLoading = false;   
                alert("Survey Complete");
                location.href = "#/view-Survey";
            }
            else  
            {
                alert("Error, Try again.");
                $scope.isLoading = false;
            }
        });
        res.error(function(html) {
            alert('Please connect to internet.');
            $scope.isLoading = false;
        });
        };
      </script>


        # This one is my html Code #
<form role="form" id="formpadd" name="formhome" class="" method="post" action="" ng-submit="formhome.$valid && surveyHomeFn()">
    <input type="hidden" ng-model="last_id" name="last_id" value="{{last_id}}" />
    <fieldset ng-repeat="choice in choices">
        <div class="span4 " ng-class="{true: 'error'}[submitted && formhome.item.$invalid]">
            <label>Items</label>
            <span class="input input--kozakura">
                <input class="input__field input__field--kozakura feildname" type="text" name="item" placeholder="Items" ng-model="choice.items"
                    ng-required="true">
                <span class="err" ng-show="(formhome.item.$dirty || submitted) && formhome.item.$error.required">item is Required</span>
            </span>
        </div>
        <div class="span4" ng-class="{true: 'error'}[submitted && formhome.no_of_cartoons.$invalid]">
            <label>No. of Cartoons</label>
            <span class="input input--kozakura">
                <input class="input__field input__field--kozakura feildname" type="number" name="no_of_cartoons" placeholder="No. of Cartoons"
                    ng-model="choice.no_of_cartoons" ng-required="true" />
                <span class="err" ng-show="submitted && formhome.no_of_cartoons.$error.required">No. of Cartoon is Required</span>
            </span>
        </div>
        <div class="span4">
            <label>Size</label>
            <span class="input input--kozakura">
                <input class="input__field input__field--kozakura feildname" type="text" name="size" placeholder="Size" ng-model="choice.size"
                />
            </span>
        </div>
        <div class="span4">
            <label>Volume</label>
            <span class="input input--kozakura">
                <input class="input__field input__field--kozakura feildname" type="text" placeholder="Volume" ng-model="choice.volume" />
            </span>
        </div>
        <button type="button" class="remove" ng-click="removeChoice($index)">
            <i class="fa fa-minus" aria-hidden="true"></i>
        </button>
        <button type="button" class="addfields remove" ng-show="$last" ng-click="addNewChoice()">
            <i class="fa fa-plus" aria-hidden="true"></i>
        </button>
    </fieldset>
    <div class="span3">
        <button class="w-button action-button" type="submit" name="surveyController" ng-show="$last" ng-click="setTab(3)">
            <i class="fa fa-arrow-circle-left fa-lg"></i> Previous</button>
    </div>
    <div class="span3">
        <button class="w-button action-button" type="submit" ng-click="submitted=true" name="surveyHome">Save </button>
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

我只需检查数组范围是否存在

if($scope.choices == 'undefined'){
    $scope.choices =  [{items: '', no_of_cartoons: '', size: '',volume: ''}];
    $scope.len = 1; 
}