【问题标题】:$localstorage vs cookies in angularjs$localstorage 与 angularjs 中的 cookie
【发布时间】:2017-05-27 23:58:08
【问题描述】:

当通过 angularjs 中的 cookie 和 localstorage 时,它​​们的意思是相同的,但仍然存在疑问,所以我的问题是:

1.cookies和localstorage的主要区别是什么?

2.我们在哪些场景下使用这些?

3.它们有什么不同?

4.举个cookie和localstorage的例子(便于理解)

【问题讨论】:

标签: angularjs cookies local-storage


【解决方案1】:

嗯,下面的链接将回答上述所有问题,除了最后一个问题。

Local Storage vs Cookies

COOKIE 演示

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

本地存储演示

<!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>

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 2012-03-17
    • 1970-01-01
    • 2015-07-23
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多