0

When through the cookies and localstorage in angularjs, they meant to be the same and still have doubt in it so, my questions are:

1.What is the main difference between cookies and localstorage?

2.On which scenario we make use of these?

3.How do they differ from each other?

4.Give an example for cookie and localstorage (for better understanding)

sree
  • 91
  • 11
  • Possible duplicate of [What is the difference between localStorage, sessionStorage, session and cookies?](http://stackoverflow.com/questions/19867599/what-is-the-difference-between-localstorage-sessionstorage-session-and-cookies) – Anirudh Mangalvedhekar Jan 12 '17 at 19:10

1 Answers1

1

Well, the following link will give answer to all the questions above except the last one.

Local Storage vs Cookies

DEMO FOR COOKIE

http://jsfiddle.net/sajeetharan/bs3paa6v/

DEMO FOR LOCAL STORAGE

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <script data-require="angular.js@1.1.5" data-semver="1.1.5" src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
    <script src="https://rawgithub.com/gsklee/ngStorage/master/ngStorage.js"></script>
    
    <script>
      angular.module('app', [
        'ngStorage'
      ]).
      
      controller('Ctrl', function(
        $scope,
        $localStorage
      ){
        $scope.$storage = $localStorage.$default({
          x: 42
        });
      });
    </script>
  </head>

  <body ng-controller="Ctrl">
    <button ng-click="$storage.x = $storage.x + 1">{{$storage.x}}</button> + <button ng-click="$storage.y = $storage.y + 1">{{$storage.y}}</button> = {{$storage.x + $storage.y}}
  </body>

</html>
Community
  • 1
  • 1
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396