3

My app should have a different default route depending on the value set in a cookie. But within my router config $cookies isn't defined - at this stage in the app's bootstrapping it seems that only $cookieProvider is defined (and similarly $cookieStore isn't available either). How do I get from this to the actual $cookies object that is accessible later by my services.

    angular.module('jnr').config(['$routeProvider', '$locationProvider', '$cookiesProvider', function($routeProvider, $locationProvider, $cookiesProvider) {

        $locationProvider.html5Mode(true).hashPrefix('!');   

        $routeProvider.when('/tunes', {
            templateUrl: '/views/list-tunes.html'
        }).when('/tunes/:instrument', {
            templateUrl: '/views/list-tunes.html'
        }).otherwise({
            redirectTo: '/tunes/' + ([get instrument from the cookie here] || 'clarinet')
        });
    }]);
};
wheresrhys
  • 22,558
  • 19
  • 94
  • 162

2 Answers2

3

It seems that you can't use in config block.

See here : Why am I unable to inject angular-cookies? and here : Why can't I get a $location injected into in my config()?

Because :

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured

And $cookies is a service and you can't inject services in config block.

You have to inject it in run block.

Or use the jquery cookie API like that :

$.cookie('myCookie')
Community
  • 1
  • 1
Thomas Pons
  • 7,709
  • 3
  • 37
  • 55
  • I should've put a comment in my question to say that at this stage $cookieStore isn't available either - only $cookieStoreProvider – wheresrhys Sep 23 '13 at 13:01
  • Why is not available ? To use it you have to download angular-cookies.js and inject it like i said – Thomas Pons Sep 23 '13 at 13:03
  • 1
    'Why' is a good question. In another service called later in the app's lifecycle $cookies is available, but I guess module.config injects dependencies in a different way, and the clear-as-mud angular docs don't make it easy to work out what the differences are exactly – wheresrhys Sep 23 '13 at 13:11
  • Yes and it's logic because Configuration blocks - get executed during the provider registrations and configuration phase. See here : http://stackoverflow.com/questions/18137662/why-cant-i-get-a-location-injected-into-in-my-config – Thomas Pons Sep 23 '13 at 13:13
  • I ended up using a function from this https://gist.github.com/steveosoule2/4073857 for my one off cookie reading task. It seems like a design flaw in angular that cookies - often used to store important config details needed on init - aren't made available on init via angular's own api. – wheresrhys Sep 23 '13 at 16:29
0

Yep. It's weird that;

  1. ng would screen that functionality from the any of configs
  2. I didn't think of the obvious answer until today (after spending several hours trying to get angular to do what it was supposed to do, as a client side framework) :-)

Just use the old tried and true: window.document.cookie

collective forehead smack...

Phillip Holmes
  • 427
  • 4
  • 10