Julio Garcia
Julio Garcia

Reputation: 1954

Using ngCookies in a ionic project

I am trying to include ngCookies in a project. The angular cookies library is included in my index.html after the ionic.bundle. I can see on the network tab of the developer tools that it is actually loading. Angular doesn't show any error when loading the page, as it usually does when a module is missing. The problem is that, when in my code I try to access the functions of the $cookies service, the $cookies variable is actually pointing to an empty object. Here are some relevant code snippets: On the definition of my app.js

angular.module('myApp', [
'ionic',
'ngCookies',
'ngMessages',
'rt.eventemitter',
'myApp.views']);

On my factory:

 angular.module('myApp.views')
   .factory('UserStore', ['$rootScope', '$q', '$cookies', '$timeout', 
        function($rootScope, $q, $cookies, $timeout){
           var user = {};
           function setSessionId(sessionId){
                console.log(">> setting sessionId to:",sessionId);
                user.sessionId = sessionId;
                $cookies.put('sessionId', user.sessionId);
           }
           return{ setSessionId:setSessionId}
        }
]);

In this case, when I try to call the setSessionId method I get an error that $cookies.put is not a function since, as I mentioned above, $cookies is just an empty object.

Any Ideas?

Upvotes: 0

Views: 1451

Answers (1)

Betty St
Betty St

Reputation: 2860

it depends on which angular version you use!

they changed a lot in angular 1.4.. in angular 1.3 when you set a cookie you can just assign it:

$cookies.sessionId = user.sessionId;

Upvotes: 1

Related Questions