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.
Use $cookies.putObject('myCookieArray',{'key1':'value1','key2':'value2'});
and getmycookiesback = $cookies.getObject('myCookieArray');
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
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