如何防止刷新后删除表单服务?
How to prevent the being deleted form service after refresh?
我在 angular 的 服务 中使用 $sessionStorage
来存储日期并在另一个页面中获取它。 (我正在使用 SPA)。
我可以在页面内完美获取数据,但是当我刷新页面时,服务中的数据被删除了。
为什么angular 从服务中删除数据?我应该怎么做才能保持
刷新页面后p它?
谢谢。
Cookie 解决方案
Whether using cookies is not a "wide" solution, it can solve your
question without a server or a database.
Take care that if the user empties temporary internet files, data will be erased, or if the cookie is not permanent, data will be erased when finish date has arrived
首先,您需要将 ngCookies 导入您的 angular 应用
bower install angular-cookies --save
之后,记得导入 angular cookies 脚本:
<script src="path/to/angular.js"></script>
<script src="path/to/angular-cookies.js"></script>
并添加依赖:
angular.module('app', ['ngCookies']);
之后,您将可以从angular调用ngCookies方法,然后存储和恢复存储在cookie中的数据:
angularJS API上的例子:
angular.module('cookiesExample', ['ngCookies'])
.controller('ExampleController', ['$cookies', function($cookies) {
// Retrieving a cookie
var favoriteCookie = $cookies.get('myFavorite');
// Setting a cookie
$cookies.put('myFavorite', 'oatmeal');
}]);
希望对您有所帮助。
Anyway, IMO, installing a server-side with a DB should be the right
solution if you really want to save info from the users.
基本上当页面刷新时,angular 重新加载,服务重新加载并且所有数据都丢失,这是 JavaScript 东西的正常行为。
基本上就像是从头开始。您需要的是一种在重新加载时保留数据的方法,您可以使用诸如本地存储之类的东西来保留和检索数据。
看看这里的一些例子:
Storing Objects in HTML5 localStorage
我在 angular 的 服务 中使用 $sessionStorage
来存储日期并在另一个页面中获取它。 (我正在使用 SPA)。
我可以在页面内完美获取数据,但是当我刷新页面时,服务中的数据被删除了。
为什么angular 从服务中删除数据?我应该怎么做才能保持 刷新页面后p它?
谢谢。
Cookie 解决方案
Whether using cookies is not a "wide" solution, it can solve your question without a server or a database.
Take care that if the user empties temporary internet files, data will be erased, or if the cookie is not permanent, data will be erased when finish date has arrived
首先,您需要将 ngCookies 导入您的 angular 应用
bower install angular-cookies --save
之后,记得导入 angular cookies 脚本:
<script src="path/to/angular.js"></script>
<script src="path/to/angular-cookies.js"></script>
并添加依赖:
angular.module('app', ['ngCookies']);
之后,您将可以从angular调用ngCookies方法,然后存储和恢复存储在cookie中的数据:
angularJS API上的例子:
angular.module('cookiesExample', ['ngCookies'])
.controller('ExampleController', ['$cookies', function($cookies) {
// Retrieving a cookie
var favoriteCookie = $cookies.get('myFavorite');
// Setting a cookie
$cookies.put('myFavorite', 'oatmeal');
}]);
希望对您有所帮助。
Anyway, IMO, installing a server-side with a DB should be the right solution if you really want to save info from the users.
基本上当页面刷新时,angular 重新加载,服务重新加载并且所有数据都丢失,这是 JavaScript 东西的正常行为。
基本上就像是从头开始。您需要的是一种在重新加载时保留数据的方法,您可以使用诸如本地存储之类的东西来保留和检索数据。
看看这里的一些例子:
Storing Objects in HTML5 localStorage