Angular Session localStorage and sessionStorage

Angular Session localStorage and sessionStorage

Today, We want to share with you Angular Session localStorage and sessionStorage.In this post we will show you localStorage and sessionStorage using AngularJS Session, hear for Introduction to localStorage and sessionStorage using anglarjs we will give you demo and example for implement.In this post, we will learn about Difference Between Local Storage, Session Storage And Cookies in angular with an example.

store data in Session using AngularJS example

An AngularJS Storage module that makes Web Storage working in the Angular js Way.
There are Contains two types of services: first = $localStorage and second $sessionStorage.

1st – Angular sessionStorage : – We all got this often-overlooked type buddy covered in storage.

READ :  PHP Application URL Routing Removing file extensions

2nd – No Cookie Fallback : – With store data (Web Storage) being readily-read data all available in all the browsers(Mozila,chrome.etc..) AngularJS officially supports in angularjs,
so such fallback is (large data)largely redundant.
AngularJS Session Storage Example

<header>
    <div id="dataidv" class="title">:#/Angularjs king</div>
    www.infinityknow.com
    <br> <br>
    Simple Session Storage Example with AngularJS
</header>
<div class="focus" id="container" ng-controller="angularkingTestCtrl">
  Hello, {{si_name}}!
</div>

var agTestApp = angular.module('agTestApp',[]);

function angularkingTestCtrl($scope,$window) {
   
    //Angularjs SAVE VALUE data
    $window.sessionStorage.setItem("parmSaveVal","I am a all value saved with (value)SessionStorage in angularJS");
    
    //Angularjs RETRIEVE VALUE all data
    $scope.si_name = $window.sessionStorage.getItem("parmSaveVal");
    
}


header{
    font-size:27px;
    text-align:center;
    margin:24px;
    padding:26px;
}

.title{
    font-size:38px;
    color:green;
}

#container{
    text-align:center;
    margin:28px;
    padding:24px;
}

OUTPUT

[code]

:#/Angularjs king
www.infinityknow.com

Simple Session Storage Example with AngularJS
Hello, I am a all value saved with (value)SessionStorage in angularJS

READ :  AngularJs Global Constants Set and Get Variables

[/code]

window.sessionStorage in AngularJS

In angularjs session storage,generally we need to keep a client session(users) state in our all angularjs application(web app).

session storage state should survive page(web page) refresh and navigations(menu) within the application(web application).

angularjs we used to ngStorage module (in storage) but lately have some changed our opinion in storage module,
as we think it is all over-engineered applay and is too heavy at runtime storage web apps.

We have store data replaced it with a simple service in angularjs that synchronizes sessionStorage data once during init mode (initialization),
and retrive data (page load )once before page unload.

Example of window.sessionStorage in AngularJS

sessionstorage.html

<!DOCTYPE html>
<html ng-app="si_app">
<head>
  <meta charset="utf-8" />
  <title>Session - angularjs session storage</title>
  <style type="text/css">
    .ng-cloak { display: none; }
  </style>
  <script src="yourprojectdir/angular.js"></script>
 <script>
//anglarjs module and factory
angular.module("si_app", []).
factory(
  "session",
  ["$window", function($window)
  {
    var session =
      angular.fromJson($window.sessionStorage.getItem("si_app")) || {};

	  //windows listener
    $window.addEventListener(
      "beforeunload",
      function()
      {
        $window.sessionStorage.setItem("si_app", angular.toJson(session));
      })
	//live function
    return session;
  }]).
controller(
  "STestdata",
  ["session",
  function(session)
  {
    this.sivalue = session;
  }]);
  </script>
</head>
<body ng-controller="STestdata as std">
  <input placeholder="your session value get" type="text" ng-model="std.sivalue.value"/>
  <a href="sessionstorage.html?p=1">Home</a>
  <a href="sessionstorage.html?p=2">About us</a>
</body>
</html> 

Store data in local storage using Angularjs

seapp.factory('adminService', ['$rootScope', function ($rootScope) {

var dataservice = {

model: {
user_name: '',
user_email: ''
},

DataSaveState: function () {
sessionStorage.adminService = angular.toJson(dataservice.model);
},

ResultState: function () {
dataservice.model = angular.fromJson(sessionStorage.adminService);
}
}

$rootScope.$on("dataSavestate", dataservice.DataSaveState);
$rootScope.$on("resultState", dataservice.ResultState);

return dataservice;
}]);

Add a Comment

Your email address will not be published. Required fields are marked *