简体   繁体   English

Angular js观看会话存储

[英]Angular js watch session Storage

I have some data from different controllers that are stored in the sessionStorga using https://github.com/fredricrylander/angular-webstorage . 我有一些来自不同控制器的数据,这些数据存储在sessionStorga中,使用https://github.com/fredricrylander/angular-webstorage The data is stored ok and it's available in the current session. 数据存储正常,并且在当前会话中可用。 The problem I am facing regards making a controller that will watch for changes in the webstorage.session I did this like this: 我面临的问题是制作一个控制器来监视webstorage.session中的变化我是这样做的:

app.controller(
    'updateDataController',
    ['$scope','$http','$sce','$location','$routeParams', '$rootScope', '$document', 'webStorage', '$interval', 'md5',
        function($scope,$http,$sce,$location,$routeParams, $rootScope, $document, webStorage, $interval, md5) {

            $scope.mySessionStorage = webStorage.session;

            $scope.$watch('mySessionStorage', function (newVal, oldVal) {
                 console.log("The web storage has changed");
            }, true);

        }
    ]);

I get the console.log output only once. 我只得到一次console.log输出。 I don't get any logs even though I change the content of webStorage.session. 即使我更改了webStorage.session的内容,我也没有得到任何日志。 What could be the problem? 可能是什么问题呢?

webStorage.session is only setting the value to $scope.mySessionStorage one time upon the initialization of your controller. webStorage.session仅在控制器初始化时将值设置为$scope.mySessionStorage一次。 So that code above the watch is only firing one time. 因此手表上方的代码只会触发一次。

Try watching the webStorage.session value instead of the value in the controller: 尝试观察webStorage.session值而不是控制器中的值:

$scope.$watch(function () {
   return webStorage.session;
}, function (newVal, oldVal) {
   console.log("The web storage has changed");
}, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM