Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store (and retrieve) a cookie array with Angular cookies

I'm trying to store my cookies with AngularJS as an array to keep the cookie file clean.

I'm using the following method:

$cookies.put('myCookieArray',{'key1':'value1','key2':'value2'});

Now, when I try to retrieve it using:

getmycookiesback = $cookies.get('myCookieArray');
console.log(getmycookiesback.key1);

I get an undefined value.

but when I try to retrieve it using this:

console.log($rootScope.getmycookiesback);

It retrieves [object Object].

What am I doing wrong? I want to get the value from key1 and key2.

like image 867
Artvader Avatar asked Apr 20 '16 10:04

Artvader


2 Answers

Use $cookies.putObject('myCookieArray',{'key1':'value1','key2':'value2'}); and getmycookiesback = $cookies.getObject('myCookieArray');

like image 131
strelok2010 Avatar answered Oct 14 '22 08:10

strelok2010


try like that:

 DemoApp.controller('DemoController', function ($cookies, $scope, $log) {

            //$cookies.put('myCookieArray',{'key1':'value1','key2':'value2'});
            $cookies['myCookieArray']= {'key1':'value1','key2':'value2'};

            getmycookiesback = $cookies['myCookieArray'];
            $log.info(getmycookiesback.key1);  
        })

here the plunker: http://plnkr.co/edit/k9fltjGUbTbfbVlAmRcJ

I hope it helps

like image 25
thegio Avatar answered Oct 14 '22 09:10

thegio