【发布时间】:2014-10-15 04:41:15
【问题描述】:
我正在尝试在页面刷新时从 sessionStorage 中检索我的搜索和过滤数据。
sessionStorage.restorestate 返回 undefined,有谁知道为什么?
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');
};
});
【问题讨论】:
-
在您的 plnkr 中似乎没有调用
sessionStorage设置代码 -
@ExplosionPills:你是对的,对不起。它在我更改路线但 sessionStorage.restorestate 始终未定义时触发。
-
plunker 不使用
ngRoute,因此不能进行任何路由更改。另外,我看不到任何将sessionStorage.restorestate设置为任何东西的代码行,所以它永远不会是"true"。是否缺少某些代码? -
@HugoWood 很好,我正试图让它与我从这篇文章中得到的东西一起工作:stackoverflow.com/a/16559855
-
赞成该答案的人可能没有测试它,因为它根本不起作用,而且远非该问题的最佳答案。 OP 只想在更改视图时保留数据。只需将数据放入服务中即可。 你想在刷新时保存。完全不同。