微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – Angular:从sessionStorage恢复范围

我试图在页面刷新时从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');
    };
  });

普兰克:http://plnkr.co/edit/oX4zygwB0bDpIcmGFgYr?p=preview

在Angular应用程序中刷新页面时,就像完全重新启动应用程序一样.因此,要从会话存储中恢复,只需在服务工厂执行时执行此操作.
app.factory('CustomerSearchService',['$rootScope',function($rootScope) {
        ...
        function restoreState() {
            service.state = angular.fromJson(sessionStorage.CustomerSearchService);
        }
        if (sessionStorage.CustomerSearchService) restoreState();
        ...
    }
]);

保存部分已经正确了.

app.factory('CustomerSearchService',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');
    };
});

DEMO

原文地址:https://www.jb51.cc/angularjs/142321.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐