2

Hello I'm wondering what is the way to use $cookieStore inside the redirectTo function of the routeprovider... I'm trying to achive this:

angular.module('App.section', ['ngCookies'], function ($httpProvider, $cookieStore) {})

  .config([
    '$routeProvider', '$cookieStore',
    function ($routeProvider, $cookieStore) {
      $routeProvider
        .when('/section/page', {
          controller: 'SectionCtrl',
          templateUrl: 'section/page.tpl.html',
          redirectTo: function (routeParams, path, search, $cookieStore) {

            console.log($cookieStore); // undefined


            return pathString;
          },
          resolve:{ ... something}
        })
    }
  ])

I want to be able to figure it out to where should I send the redirection depending on cookiestore.

raulricardo21
  • 2,499
  • 4
  • 20
  • 27

1 Answers1

1

You can't do this. $cookieStore is a service, you could use only providers during config phase. Move you logic to run method.

Or you could fire some event on root scope in the 'redirectTo' function, then subscribe to this event on the run phase and redirect user depending on his cookies.

user854301
  • 5,383
  • 3
  • 28
  • 37
  • So far I understand that I can't use $cookieStore because is a service. But I didn't quite understand your suggestions. What I try to accomplish is to redirect if I can't find certain cookie, but I think redirectTo is the better place to do that, instead of forcing redirection on a controller for example. – raulricardo21 Sep 06 '13 at 20:00