angular-local-storage
angular-local-storage copied to clipboard
Missing documentation
I'm trying to save a value in a cookie. I'm not sure what angular-local-storage
is meant to do different from angular-cookie
.
I've tried to save the cookie with the following code:
localStorageService.clearAll();
localStorageService.set('test_1','test-value');
localStorageService.add('test_2', 'test-value');
localStorageService.cookie.add('test_3', 'test-value');
localStorageService.cookie.set('test_4', 'test-value');
None of the previous command are settings the cookie value. What am I missing?
Why should I use angular-local-storage
instead of angular-cookie
?
Cheers
Looks like you have discovered a bug.
Angular Local Storage is meant to use modern local storage APIs within the browser like Local Storage and Session Storage and fall back to use cookies for older browsers. It is not meant for managing cookies on it's own, though normally it should work for getting and setting.
@grevory has cookie getting and setting always been broken?
// Directly get a value from a cookie
// Example use: localStorageService.cookie.get('library'); // returns 'angular'
var getFromCookies = function (key) {
if (!browserSupportsCookies()) {
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
return false;
}
var cookies = $document.cookie && $document.cookie.split(';') || [];
for(var i=0; i < cookies.length; i++) {
var thisCookie = cookies[i];
while (thisCookie.charAt(0) === ' ') {
thisCookie = thisCookie.substring(1,thisCookie.length);
}
if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) {
return decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length));
}
}
return null;
};
$document returns a jquery wrapped object that doesn't have access to cookie, accessing $document via a DOM element will have access however. $document[0].cookie