Retain Session value in jsp page using Angularjs

时间:2018-03-29 13:58:15

标签: angularjs session angular-ngmodel

I'm using Angularjs and Spring .....

I had set the Session value for certain text fields using angularjs sessionstorage . When is reload the page or move backwards from other jsp page those values must retain in respective fields

$window.sessionStorage.setItem('namednewicno',$scope.user.namednewicno);

This is the session value for namednewicno

I need to display that value using data-ng-model

This is my controller

app.controller('FormSubmitController',[ '$scope', '$http','$window', function($scope, $http,$window) {

$scope.user = [];
    $scope.headerText = 'AngularJS Post Form Spring MVC example: Submit below form';


    $scope.submit = function() {            
        $window.localStorage.setItem('namednewicno',$scope.user.namednewicno);              
        $window.sessionStorage.setItem('namednewicno',$scope.user.namednewicno);




        var formData = {
                "namednewicno" : $scope.user.namednewicno,
                "namedoldicno" : $scope.user.namedoldicno,
                "namedage" : $scope.user.namedage,
                "nameddriverexperience" : $scope.user.nameddriverexperience,
                "namedgender" : $scope.user.namedgender,
                "nameddrivername" : $scope.user.nameddrivername,
                "nameddriverrelationship" : $scope.user.nameddriverrelationship 
        };
        alert("controller");
        var response = $http.post('PostFormData', formData);
        response.success(function(data, status, headers, config) {
            $scope.user.push(data);
            alert("success");
        });
        response.error(function(data, status, headers, config) {
            alert( "Exception details: " + JSON.stringify({data: data}));
        });
        $scope.user.namednewicno=$window.sessionStorage.getItem( 'namednewicno' );
        console.log($window.sessionStorage.getItem( 'namednewicno' ));

        //Empty list data after process
    //  $scope.user = [];

    };


}]);

1 个答案:

答案 0 :(得分:0)

app.controller('NamedPopup',[ '$scope', '$http','$window', function($scope, $http,$window) {

$scope.user = [];       
        $scope.user.namednewicno=$window.sessionStorage.getItem( 'namednewicno' );


    $scope.submit = function() {            
        $window.sessionStorage.setItem('namednewicno',$scope.user.namednewicno);



        var formData = {
                "namednewicno" : $scope.user.namednewicno
        };
        var response = $http.post('saveNamedDrivers', formData);
        response.success(function(data, status, headers, config) {
            $scope.user.push(data);
            //alert("success");
        });
        response.error(function(data, status, headers, config) {
            alert( "Exception details: " + JSON.stringify({data: data}));
        });         
        //Empty list data after process
    $scope.user = [];

    };

}]);

Html代码必须像这样才能在html上保留会话

<input ng-value="{{user.namednewicno}}" 
       ng-model="user.namednewicno" 
       required 
       class="form-control" type="number" />