Han Dijk
Han Dijk

Reputation: 1612

Access cookies in angularJs service

I can succesfully access cookies on my controller like this.

angular.module('mobbr', [ 'ngCookies' ]).    

function RegisterCtrl($scope, $cookies) { }

But whenever i try to use cookies in a service like this.

angular.module('mobbr.services', []);
angular.module('mobbr.services').factory('currentUser', [ 'ngCookies', function ($cookies) {}]);

I get the following error: ngCookiesProvider <- ngCookies <- currentUser.

Any thoughts on why this won't work and how i should initialize a service with acces to cookies?

Upvotes: 5

Views: 6582

Answers (2)

SharpCoder
SharpCoder

Reputation: 19189

For me this worked:

module.controller('myCtrl', ['$scope', '$cookies',
        function($scope, $cookies) {
             ...........
         }
    ]);

Instead of using ngCookies I am using $cookies. I looked at this example but somehow it was throwing error Unknown provider: ngCookiesProvider <- ngCookies

Upvotes: 1

JeffB
JeffB

Reputation: 1867

This is what my code for something similar looks like:

angular.module('app.MyData', ['ngResource','ngCookies']).
    factory('MyService', function($resource, $http, $cookies) {
        ...
    })

Upvotes: 11

Related Questions