解决方法
With local storage,web applications can store data locally within the user’s browser. Before HTML5,application data had to be stored in cookies,included in every server request. Local storage is more secure,and large amounts of data can be stored locally,without affecting website performance. Unlike cookies,the storage limit is far larger (at least 5MB) and information is never transferred to the server. Local storage is per origin (per domain and protocol). All pages,from one origin,can store and access the same data.
它为您提供了访问存储的对象 – window.localStorage和window.sessionStorage
window.localStorage – 存储没有过期日期的数据
window.sessionStorage – 存储一个会话的数据,因此在关闭浏览器选项卡时数据会丢失
要检索数据,您可以执行类似的操作
localStorage.getItem("lastname");
因此,如果你想在角度中这样做,你会使用$window服务,但它的行为与上面的例子相同.
要解决ngStorage:
An AngularJS module that makes Web Storage working in the Angular Way. Contains two services: $localStorage and $sessionStorage. No dealing with getters and setters like you have to in $window service
您可以在$scope下通过引用传递$localStorage或$sessionStorage:
$scope.$storage = $localStorage;
然后你可以使用$storage作为和其他角度变量
<body ng-controller="Ctrl"> <button ng-click="$storage.counter = $storage.counter + 1">{{$storage.counter}}</button> </body>
如果您将在webapp中使用angularjs,我会使用ngStorage,因为您会更熟悉并熟悉语法.这只是我的观点.