Often we need to keep a client session state in our angularjs application.
This state should survive page refresh and navigations within the application.
Earlier we used ngStorage module but lately have changed our opinion, as we think it's over-engineered and is too heavy at runtime.
We have replaced it with a simple service that synchronizes sessionStorage once during initialization, and once before page unload.
Look at an example (session.html):
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Session</title> <style type="text/css"> .ng-cloak { display: none; } </style> <script src="angular.js"></script> <script> angular.module("app", []). factory( "session", ["$window", function($window) { var session = angular.fromJson($window.sessionStorage.getItem("app")) || {}; $window.addEventListener( "beforeunload", function() { $window.sessionStorage.setItem("app", angular.toJson(session)); }) return session; }]). controller( "Test", ["session", function(session) { this.state = session; }]); </script> </head> <body ng-app="app" ng-controller="Test as test"> <input type="text" ng-model="test.state.value"/> <a href="session.html?p=1">Page 1</a> <a href="session.html?p=2">Page 2</a> </body> </html>
Source can be found at nesterovsky-bros/angularjs-api/services/session.html.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u