angular-local-storage icon indicating copy to clipboard operation
angular-local-storage copied to clipboard

Missing documentation

Open Maxwell2022 opened this issue 10 years ago • 2 comments

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

Maxwell2022 avatar May 08 '14 05:05 Maxwell2022

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 avatar May 08 '14 06:05 grevory

@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

chemoish avatar Sep 08 '14 23:09 chemoish