admin管理员组

文章数量:1416083

I have some data from different controllers that are stored in the sessionStorga using . 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:

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. I don't get any logs even though I change the content of webStorage.session. What could be the problem?

I have some data from different controllers that are stored in the sessionStorga using https://github./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:

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. I don't get any logs even though I change the content of webStorage.session. What could be the problem?

Share Improve this question asked Sep 26, 2014 at 13:58 exilonXexilonX 1,7613 gold badges27 silver badges51 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

webStorage.session is only setting the value to $scope.mySessionStorage one time upon the initialization of your controller. So that code above the watch is only firing one time.

Try watching the webStorage.session value instead of the value in the controller:

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

本文标签: javascriptAngular js watch session StorageStack Overflow