Introduction

Many of you might already know that each browser has its own local storage. Using this functionality we can store user data in the Browser Local Storage rather than sending it to the server. This scenario is best seen in mobile applications. The application stores data in the browser's local storage when the app goes offline and sends the data to the server when it gets connected.

The data remains stored with the browser even after it is closed or the computer is restarted until the browser cache is forcefully cleared.

In this demo I will show how to save and retrieve the data from browser local storage and not detecting the connectivity when it goes online.
Various ways of Browser Local Storage

1. Web/Local Storage
: Used for storing name / value pairs just like cookies. (Used in this demo). Browser Compatibility:
2. Web SQL Database: Used for structured databases with all the functionality and complexity of a typical SQL-powered relational database.
Firefox and IE do not support it.
3. IndexedDB: Is a collection of "object stores" that you can just drop objects into. The stores are something like SQL tables, but in this case, there's no constraints on the object structure and so no need to define anything upfront.

It uses the best functionality of Local and IndexedDB.

Other ways (Session Storage, Cookies).
  • Pros: Good Performance, decent browser Support.
  • Cons: Complex API


Getting Started
  1. Create a new Empty Web Project in Visual Studio.

  2. Install AngularJs from Nuget Package Manager.

    (Tools > Nuget Packager Manager > Package Manager Console > Install-Package AngularJs).

  3. Code the default.aspx page.
Include AngularJs reference.

The Script
  1. <script type="text/javascript" src="Scripts/Angular.min.js"></script>
Create the LocalStorage Service Module to read and write the data.
  1. //Create the AngularJS module named StorageService
  2. //Create getLocalStorage service to access UpdateEmployees and getEmployees method
  3. var storageService = angular.module('storageService', []);
  4. storageService.factory('getLocalStorage', function () {
  5. var employeeList = {};
  6. return {
  7. list: employeeList,
  8. updateEmployees: function (EmployeesArr) {
  9. if (window.localStorage && EmployeesArr) {
  10. //Local Storage to add Data
  11. localStorage.setItem("employees", angular.toJson(EmployeesArr));
  12. }
  13. employeeList = EmployeesArr;
  14. },
  15. getEmployees: function () {
  16. //Get data from Local Storage
  17. employeeList = angular.fromJson(localStorage.getItem("employees"));
  18. return employeeList ? employeeList : [];
  19. }
  20. };
  21. });
Create an AngularJs Employee module and register with the storageService. Add the other functionalities to add, delete and count employees.
  1. // Create the AngularJS module Employees and Register the storageService with it
  2. var app = angular.module('Employees', ['storageService']);
  3. // Create the Controller EmployeeController
  4. app.controller('EmployeesController', ['$scope', 'getLocalStorage', function ($scope, getLocalStorage) {
  5. $scope.appTitle = "LocalStorage Demo";
  6. $scope.appHeadline = "AngularJS and HTML5";
  7. //Read the Employee List from LocalStorage
  8. $scope.employees = getLocalStorage.getEmployees();
  9. //Count the Employee List
  10. $scope.count = $scope.employees.length;
  11. //Add Employee - using AngularJS push to add Employee in the Employee Object
  12. //Call Update Employee to update the locally stored Employee List
  13. //Reset the AngularJS Employee scope
  14. //Update the Count
  15. $scope.addEmployee = function () {
  16. $scope.employees.push({ 'empno': $scope.empno, 'empname': $scope.empname, 'empsalary': $scope.empsalary});
  17. getLocalStorage.updateEmployees($scope.employees);
  18. $scope.empno = '';
  19. $scope.empname = '';
  20. $scope.empsalary = '';
  21. $scope.count = $scope.employees.length;
  22. };
  23. //Delete Employee - Using AngularJS splice to remove the emp row from the Employee list
  24. //All the Update Employee to update the locally stored Employee List
  25. //Update the Count
  26. $scope.deleteEmployee = function (emp) {
  27. $scope.employees.splice($scope.employees.indexOf(emp), 1);
  28. getLocalStorage.updateEmployees($scope.employees);
  29. $scope.count = $scope.employees.length;
  30. };
  31. }]);
The HTML
Add the ng-app to access AngularJs functionalities.
  1. <body ng-app="Employees">
Add the Controller to access the Employee Service functionalities.
  1. <div ng-controller="EmployeesController">
Add the Form and bind input controls with the employee fields using ng-model. Add the required attribute to the fields that you want to be required.
  1. <form name="frm">
  2. <table>
  3. <tr>
  4. <td>Emp No :
  5. </td>
  6. <td>
  7. <input type="text" name="empno" ng-model="empno" required><br />
  8. </td>
  9. </tr>
  10. <tr>
  11. <td>Emp Name :
  12. </td>
  13. <td>
  14. <input type="text" name="empname" ng-model="empname" required>
  15. </td>
  16. </tr>
  17. <tr>
  18. <td>Emp Salary :
  19. </td>
  20. <td>
  21. <input type="text" name="empsalary" ng-model="empsalary">
  22. </td>
  23. </tr>
  24. <tr>
  25. <td colspan="2" align="right">
  26. <button ng-click="addEmployee()" ng-disabled="frm.$invalid">Add Employee</button>
  27. </td>
  28. </tr>
  29. </table>
  30. lt;/form>
Add the table to display the Employee List and Delete button:
  1. <table cellpadding="4" cellspacing="4" border="1" style="border-collapse: collapse; border: solid 1px #000">
  2. <tr>
  3. <td><b>Emp No</b></td>
  4. <td><b>Emp Name</b></td>
  5. <td><b>Emp Salary</b></td>
  6. <td><b>Action</b></td>
  7. </tr>
  8. <tr ng-repeat="employee in employees">
  9. <td>{{ employee.empno }}
  10. </td>
  11. <td>{{ employee.empname }}
  12. </td>
  13. <td>{{ employee.empsalary }}
  14. </td>
  15. <td>
  16. <button ng-click="deleteEmployee(employee)">Delete</button>
  17. </td>
  18. </tr>
  19. </table>
  20. <div>Total Employees : {{count}}</div>
  21. </div>
The full Code
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4. <title></title>
  5. <!--[if lte IE 8]>
  6. document.write("NOT HTML5 Compatible!!");
  7. <![endif]-->
  8. <script type="text/javascript" src="Scripts/Angular.min.js"></script>
  9. <script type="text/javascript">
  10. var isHtml5Compatible = document.createElement('canvas').getContext != undefined;
  11. if (isHtml5Compatible) {
  12. initiateLocalStorage();
  13. }
  14. function initiateLocalStorage() {
  15. // Create the AngularJS app
  16. var app = angular.module('Employees', ['storageService']);
  17. // Create the Controller
  18. app.controller('EmployeesController', ['$scope', 'getLocalStorage', function ($scope, getLocalStorage) {
  19. $scope.appTitle = "LocalStorage Demo";
  20. $scope.appHeadline = "AngularJS and HTML5";
  21. //Read the Employee List from LocalStorage
  22. $scope.employees = getLocalStorage.getEmployees();
  23. //Count the Employee List
  24. $scope.count = $scope.employees.length;
  25. //Add Employee - using AngularJS push to add Employee in the Employee Object
  26. //Call Update Employee to update the locally stored Employee List
  27. //Reset the AngularJS Employee scope
  28. //Update the Count
  29. $scope.addEmployee = function () {
  30. $scope.employees.push({ 'empno': $scope.empno, 'empname': $scope.empname, 'empsalary': $scope.empsalary });
  31. getLocalStorage.updateEmployees($scope.employees);
  32. $scope.empno = '';
  33. $scope.empname = '';
  34. $scope.empsalary = '';
  35. $scope.count = $scope.employees.length;
  36. };
  37. //Delete Employee - Using AngularJS splice to remove the emp row from the Employee list
  38. //All the Update Employee to update the locally stored Employee List
  39. //Update the Count
  40. $scope.deleteEmployee = function (emp) {
  41. $scope.employees.splice($scope.employees.indexOf(emp), 1);
  42. getLocalStorage.updateEmployees($scope.employees);
  43. $scope.count = $scope.employees.length;
  44. };
  45. }]);
  46. //Create the Storage Service Module
  47. //Create getLocalStorage service to access UpdateEmployees and getEmployees method
  48. var storageService = angular.module('storageService', []);
  49. storageService.factory('getLocalStorage', function () {
  50. var employeeList = {};
  51. return {
  52. list: employeeList,
  53. updateEmployees: function (EmployeesArr) {
  54. if (window.localStorage && EmployeesArr) {
  55. //Local Storage to add Data
  56. localStorage.setItem("employees", angular.toJson(EmployeesArr));
  57. }
  58. employeeList = EmployeesArr;
  59. },
  60. getEmployees: function () {
  61. //Get data from Local Storage
  62. employeeList = angular.fromJson(localStorage.getItem("employees"));
  63. return employeeList ? employeeList : [];
  64. }
  65. };
  66. });
  67. }
  68. </script>
  69. </head>
  70. <body ng-app="Employees">
  71. <div ng-controller="EmployeesController">
  72. <h1 class="app-title">{{ appTitle }}</h1>
  73. <h1 class="app-headline">{{ appHeadline }}</h1>
  74. <form name="frm">
  75. <table>
  76. <tr>
  77. <td>Emp No :
  78. </td>
  79. <td>
  80. <input type="text" name="empno" ng-model="empno" required><br />
  81. </td>
  82. </tr>
  83. <tr>
  84. <td>Emp Name :
  85. </td>
  86. <td>
  87. <input type="text" name="empname" ng-model="empname" required>
  88. </td>
  89. </tr>
  90. <tr>
  91. <td>Emp Salary :
  92. </td>
  93. <td>
  94. <input type="text" name="empsalary" ng-model="empsalary">
  95. </td>
  96. </tr>
  97. <tr>
  98. <td colspan="2" align="right">
  99. <button ng-click="addEmployee()" ng-disabled="frm.$invalid">Add Employee</button>
  100. </td>
  101. </tr>
  102. </table>
  103. </form>
  104. <table cellpadding="4" cellspacing="4" border="1" style="border-collapse: collapse; border: solid 1px #000">
  105. <tr>
  106. <td><b>Emp No</b></td>
  107. <td><b>Emp Name</b></td>
  108. <td><b>Emp Salary</b></td>
  109. <td><b>Action</b></td>
  110. </tr>
  111. <tr ng-repeat="employee in employees">
  112. <td>{{ employee.empno }}
  113. </td>
  114. <td>{{ employee.empname }}
  115. </td>
  116. <td>{{ employee.empsalary }}
  117. </td>
  118. <td>
  119. <button ng-click="deleteEmployee(employee)">Delete</button>
  120. </td>
  121. </tr>
  122. </table>
  123. <div>Total Employees : {{count}}</div>
  124. </div>
  125. </body>
  126. </html>
Output


The data to be added will remain stored even after the browser is closed.