2015-12-02 148 views
0

我不明白爲什麼我無法獲取我的cookie?無法獲取cookie值

$ rootScopt.globals.currentUser =未定義

angular.module('MyModule', [ 
    'Authentication', 
    'Home', 
    'ngRoute', 
    'ngCookies' 
]) 
.config([ 
    //Config 
]) 
.run([ 
    '$rootScope', '$location', '$cookies', '$http', 
    function($rootScope, $location, $cookies, $http) { 
     //Keep user logged in after page refresh 
     $rootScope.globals = { 
      currentUser: { 
       username: "foo", 
       password: "bar" 
      } 
     } 

     $cookies.put('globals', $rootScope.globals); 

     $rootScope.globals = $cookies.get('globals') || {}; 



     if ($rootScope.globals.currentUser) { //Never go in here.. 
      //Do stuff 
     } 
    } 
]); 

我檢查了,我的cookie獲取存儲,它似乎很好地工作。但是,當試圖讓我的cookie不起作用。任何人現在爲什麼?

+0

是U使用的是什麼瀏覽器? – vivid

回答

1

默認情況下,Chrome不允許本地文件使用Cookie。您必須使用--enable-file-cookies標誌運行它。

試試這個方法: jsfiddle

angular.module('test',['ngCookies']) 
    .run(function($rootScope, $cookies){ 
    $rootScope.globals = { 
      currentUser: { 
       username: "foo", 
       password: "bar" 
      } 
     } 
    $cookies.put('globals', JSON.stringify($rootScope.globals)); 
    var a = $cookies.get('globals'); 
    $rootScope.globals = JSON.parse(a) || {} 
    console.log($rootScope.globals); 
}) 
+0

但我可以看到cookie被存儲..? – MrProgram

+0

@emptyman檢查編輯 – vivid