Cookies for Angularjs - how to use them through controllers

There seems to be no really clear documentation on using cookies with AngularJS, so I lost this a bit.

I have two controllers, one creates a cookie and saves the user ID, and then I want to get this ID later when the other controller is working. I think I successfully created the cookie and saved the value for id, however, it seems I can’t return the identifier from the cookie in the second controller. I get an error in the console when I try to read the identifier:

TypeError: 'undefined' is not an object

PS: I work in Xcode since this is an iOS app for iPhone.

function firstCtrl($scope, $cookieStore) {
    $scope.connectToFacebook = function() {
        FB.api('/me', function(response, data, status, headers, config) {
        var fbid=response.id;
        $cookieStore.put('id', fbid);
        console.log($cookieStore.get('id')); //This correctly displays the users FB id
        });
    }
}

function secondCtrl($scope, $cookieStore) {
    $scope.submit = function() {
    console.log($cookieStore.get('id')); // This is currently displaying: TypeError: 'undefined' is not an object
    };
}
+5
source share
1 answer

Greg took your advice and went instead of localstorage.

, , : https://github.com/grevory/angular-local-storage

cookie, localstorage

: http://gregpike.net/demos/angular-local-storage/demo.html

+7

All Articles