0

I'm testing the cookies of angularjs. Am I wondering why it's returning undefined in the console? When I set or save value using the $cookieStore.put() is working then when in getting the value from the cookie it's returning undefined.

<!DOCTYPE html>
<head  runat="server">
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular-cookies.js"></script>
    <script>
        angular.module('app', ['ngCookies']).controller('MyController', ['$scope','$cookieStore', function($scope, $cookieStore) {

            $scope.WriteCookie = function() {           
                $cookieStore.put('visited', $scope.yes);                
                console.log($cookieStore.get('visited'));
            }       

            $scope.ReadCookie = function() {
                $cookieStore.get('visited');
                console.log($cookieStore.get('visited'));
            }

        }]);
    </script>
</head>
<body ng-app="app">
    <form>
    <div ng-controller="MyController">      
    <input type="button" value="Write Cookie" ng-click="WriteCookie()" />
    <input type="text" ng-model="yes" />

    <input type="button" value="Read Cookie" ng-click="ReadCookie()" />
    <input type="text" ng-model="cookie" />
    </div>
    </form>
</body>
</html>
Chuck
  • 29
  • 6
  • You don't appear to have an opening `` tag – Phil Sep 25 '17 at 00:57
  • @Phil just the same nothing happens it's undefined. – Chuck Sep 25 '17 at 00:59
  • Seems to work just fine ~ http://plnkr.co/edit/hqbFDzuCB1N9ZTUsq321?p=preview. I did add `$scope.cookie = $cookieStore.get('visited')` so you could see the value – Phil Sep 25 '17 at 01:01
  • weird on my local machine, nothing happen – Chuck Sep 25 '17 at 01:02
  • You never assign it to `$scope.cookie` in order to pass it to `ng-model`. Beyond that it works fine – charlietfl Sep 25 '17 at 01:03
  • @charlietfl just the same it's undefined $scope.ReadCookie = function() { $scope.cookie = $cookieStore.get('visited'); console.log($cookieStore.get('visited')); } – Chuck Sep 25 '17 at 01:05
  • Note that $cookieStore was deprecated in `1.4` ... try upgrade and use `$cookies` perhaps. https://docs.angularjs.org/api/ngCookies/service/$cookieStore – charlietfl Sep 25 '17 at 01:09
  • sorry, but the required version is 1.3.9 – Chuck Sep 25 '17 at 01:11
  • @Chuck are you loading the page via `file:///`? Possible duplicate of [Why does Chrome ignore local jQuery cookies?](https://stackoverflow.com/questions/335244/why-does-chrome-ignore-local-jquery-cookies) – Phil Sep 25 '17 at 02:37

2 Answers2

1

Try like this :

update cookie version 1.6.6 and use $cookies instead of $cookieStore

angular.module('myApp', ['ngCookies'])
.controller('myController', ['$cookies', function($cookies) {

  $cookies.put('myFavorite', 'oatmeal');
  var favoriteCookie = $cookies.get('myFavorite');

  console.log('favoriteCookie', favoriteCookie);

}]);
Chandru
  • 10,864
  • 6
  • 38
  • 53
0

I tested your code two ways. Opening it as file and from server. In the first case I get 'undefined' whereas in second case it reads the cookie as expected. I did some digging and looks like some browser including Chrome does not allow setting cookies for a page opened as a file. More info ...

yesIcan
  • 1,441
  • 1
  • 11
  • 18