javascript - Angular js watch session Storage - Stack Overflow

I have some data from different controllers that are stored in the sessionStorga using . The data is st

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745245116a4618375.html

相关推荐

  • javascript - Angular js watch session Storage - Stack Overflow

    I have some data from different controllers that are stored in the sessionStorga using . The data is st

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信