0

I am using angular-permission and try to redirect users according to their roles.

I am trying to make something like this, but am not able to inject rootScope inside app.config to get the user:

redirectTo: function(){  
   if($rootScope.isAdmin){

   }
   else{

   }                      
   return 'login';
}

and here's the config section inside my app.js file

.config(['$stateProvider', '$locationProvider', '$datepickerProvider', '$timepickerProvider', 'RestangularProvider', '$httpProvider', '$scrollspyProvider', function ($stateProvider, $locationProvider, $datepickerProvider, $timepickerProvider, Restangular, $httpProvider, $scrollspyProvider) {
    $stateProvider
        .state('test', {
            url: '/test',
            templateUrl: "/test.html",
            data: {
                title: 'test',
                pageTitle: 'test',
                access: 'authenticated',
                permissions: {
                    only: ['admin'],
                    redirectTo: function(){
                        //////////////////////////////////
                    }
                }
            }
        })
}])

Is there a different way to do this ?

Dev
  • 1,861
  • 2
  • 14
  • 20

1 Answers1

2

Without seeing your app.config - I can tell you that the best place to do this is probably in app.run instead of app.config.

app.run(['$rootScope', function($rootScope){
// stuff
}]);

Here is the source confirming that you can't use $rootScope in .config. Only in .run. https://stackoverflow.com/a/10489658/1543447

Community
  • 1
  • 1
itamar
  • 3,837
  • 5
  • 35
  • 60
  • I added my app.config now – Dev Feb 17 '16 at 16:01
  • Please try my suggestion. I added a link to another answer that shows that you can't use $rootScope in config - only in .run. Try it and let me know if it works. – itamar Feb 18 '16 at 11:33