AngularJs - $digest() iterations reached with $cookieStore

73 views Asked by At
app.controller('appCtrl', function ($scope, $cookieStore) {
$scope.$watch(function(){return $cookieStore.get('currentUser');},   function(newValue, oldValue){
    if(newValue !== oldValue){
            $scope.currentUser = $cookieStore.get('currentUser');
        }
    });
});

I have developed the code above with the intention to watch value saved in $cookieStore. When user singed in successfully, the json object will be saved in $cookieStore, with the help of this $watch function, user information would display on the top corner of the page.

$cookieStore.put('currentUser', response);

I am having two issues with this solution:

  1. the $watch function does not update the $scope.currentUser as I was hoping to. It only gets updated when I refresh the whole web page.
  2. Somewhere in this solution, the $digest() functions was called repeatedly. I tried to resolve this problem by adding if(newValue !== oldValue){}, it does not work.

If I change my solutions to use $cookies instead of $cookieStore, it seems to be all working as expected, but $cookies does not allow me to save a Json object, which is why I prefer to use $cookieStore instead.

Any ideas? Really appreciated it!

0

There are 0 answers