I'm using AngularJS 1.4.0 and the $cookies
service. The following code will always print out an empty object:
(function() {
var app = angular.module("user-cookies-service", []);
app.service('UserCookiesService', ["$cookies", function($cookies) {
$cookies.put("Hello", "World");
console.log($cookies.getAll());
}]);
})();
I've tried:
$cookieStore
where cookies don't persist when the browser is refreshed (but it at least created the cookies).I haven't been able to find a similar issue anywhere.
Thank you.
A little late but for anyone having the same issue. I had a similar problem. You have to make sure you are passing the correct options as the third parameter. In my case the path was not being set. Angular uses your <base>
tag to set the path. If that is not available for whatever reason, it will not set the cookie.
try this
$cookies.put('secret', 'I cant tell you' {
expires: new Date(date),
path: '/'
});
For anyone who stumbles upon this problem...
By default, Chrome doesn't allow cookies for local files. You have to run it with the --enable-file-cookies
flag.
Make sure Chrome is closed and open the Windows Command Prompt/Terminal and run the following command:
<path-to-chrome>/chrome.exe --enable-file-cookies
This will allow you to put/get cookies locally.
Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With