I am currently testing the cookie functionality in AngularJS. I'm encountering an issue where the console is returning 'undefined' when trying to retrieve a saved value from the cookie using $cookieStore.put(). It seems to work fine when setting or saving the value, but retrieving it returns 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>