本文介绍了角:从sessionStorage的恢复范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从sessionStorage的找回我的搜索和过滤数据时,页面刷新。
I am trying to retrieve my search and filter data from sessionStorage when the page refreshes.
sessionStorage.restorestate返回undefined,没有人知道为什么吗?
sessionStorage.restorestate returns undefined, does anyone know why?
app.run(function($rootScope) {
$rootScope.$on("$routeChangeStart", function(event, next, current) {
if (sessionStorage.restorestate == "true") {
$rootScope.$broadcast('restorestate'); //let everything know we need to restore state
sessionStorage.restorestate = false;
}
});
//let everthing know that we need to save state now.
window.onbeforeunload = function(event) {
$rootScope.$broadcast('savestate');
};
});
Plunkr:
推荐答案
当您刷新页面在角应用程序,它就像完全重新启动应用程序。所以从会话存储恢复,只是做服务工厂执行时。
When you refresh the page in an Angular app, it is like completely rebooting the application. So to restore from the session storage, just do it when the service factory executes.
app.factory('CustomerSearchService', ['$rootScope',
function($rootScope) {
...
function restoreState() {
service.state = angular.fromJson(sessionStorage.CustomerSearchService);
}
if (sessionStorage.CustomerSearchService) restoreState();
...
}
]);
的保存部分已经是正确的。
The saving part was already correct.
app.factory('CustomerSearchService', ['$rootScope',
function($rootScope) {
...
function saveState() {
sessionStorage.CustomerSearchService = angular.toJson(service.state);
}
$rootScope.$on("savestate", saveState);
...
}
]);
app.run(function($rootScope) {
window.onbeforeunload = function(event) {
$rootScope.$broadcast('savestate');
};
});
这篇关于角:从sessionStorage的恢复范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!