How to use SessionStorage in Angular JS

How to use SessionStorage in Angular JS

SessionStorage is similar to localstorage, except that data is stored in sessionstorage but it expired. When data is stored in localstorage, data will not expire even when the browser will end ( i. e when the browser is closed )
The sessionStorage is cleared, if we close the tab, Close the browser. If we set data in chrome that data is not available on Firebox. Even that that data is not available on chrome next tab.  

 

Exmaple :

 

<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/ngstorage/0.3.6/ngStorage.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', ["ngStorage"])
        app.controller('MyController', ['$scope', '$localStorage', '$sessionStorage', '$window',function ($scope, $localStorage, $sessionStorage, $window) {
            $scope.Save = function () {
                $localStorage.LocalMessage = $scope.myname;
            }
            $scope.Get = function () {
                $window.alert($sessionStorage.SessionMessage);
            }
        }]);
    </script>
    <div ng-app="MyApp" ng-controller="MyController">
<input type="text" name="myname" ng-model="myname">
        <input type="button" value="Save" ng-click="Save()" />
        <input type="button" value="Get" ng-click="Get()" />
    </div>
</body>
</html>

0 Comments

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed

>