angularjs - Angular $cookieStore not retaining updated cookie on refresh -


i'm having issues $cookiestore retaining cookie value after updating it. here 2 methods of userservice deals cookie:

var getcurrentuser = function () {     return $cookiestore.get('currentuser'); };  var updatecurrentuser = function () {     return $http.get(baseurl + 'api/session').then(function (response) {         $cookiestore.put('currentuser', response.data);         $rootscope.$broadcast('currentuser', response.data);     }, function (response) {         $cookiestore.remove('currentuser');         $rootscope.$broadcast('currentuser', null);     }); }; 

throughout app, after action executed affect current user's meta data, call userservice.updatecurrentuser() retrieves latest user data server , updates cookie. then, in places display user data, have following code update user model in particular controller:

$scope.$on('currentuser', function (event, data) {     $scope.user = data; }); 

as step through code, appears working correctly. after $cookiestore.put('currentuser', response.data); line runs, updated value can confirmed checking $cookiestore.get('currentuser'). when check actual cookie using browser tool, however, cookie value not updated. i'm not sure if browser tool requires refresh show new data. when refresh page, updated cookie value no seen. going on?

thanks in advance.

check out documentation adding cookie using $cookie service:

put(key, value, [options]); 

the third argument allows additional options:

  • path (string)
  • domain (string)
  • expires (date)
  • secure (boolean)

you should set "expires" define when cookie should expire, otherwise cookie expire when refresh or leave site.

$cookies.put("id", 1, {     expires: new date(2016, 1, 1) }); 

also service called $cookies. since angular 1.4 can set expiry. until wasn't possible.

http://docs.angularjs.org/api/ngcookies/service/$cookies


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -