使用$ cookieStore

时间:2015-06-14 11:01:34

标签: javascript angularjs angular-cookies

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

我开发了上面的代码,目的是观察保存在$ cookieStore中的值。当用户成功烧录时,json对象将保存在$ cookieStore中,借助此$ watch函数,用户信息将显示在页面的顶角。

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

我对这个解决方案有两个问题:

  1. $ watch函数不会像我希望的那样更新$ scope.currentUser。它只在我刷新整个网页时才会更新。
  2. 在此解决方案的某处,重复调用$ digest()函数。我尝试通过添加 if(newValue!== oldValue){} 来解决此问题,但它不起作用。
  3. 如果我改变我的解决方案使用$ cookies而不是$ cookieStore,它似乎都按预期工作,但$ cookies不允许我保存Json对象,这就是我更喜欢使用$ cookieStore的原因。

    有什么想法吗?非常感谢!

0 个答案:

没有答案