Search code examples
angularjsroute-provider

AngularJS: Config $routeProvider after $http request


I need to config $routeProvider after ajax request, but i can not using $http in config, means following code causes error:

module.config(['$routeProvider','$http',function ($routeProvider,$http) {}

and $routeProvider can not accessed in controller!

I need this approach for considering who can see what page as index page.


Solution

  • You can pass $routeProvider from config object to your controller. For example

    in your route config file you write:

     .when('/', {
                        templateUrl: 'template.html',
                        controller: 'MyController',
                        controllerAs: 'main',
                        resolve: {
                            myRouteConfig: function(){
                                return $routeProvider;
                            }
                        }
                    })
    

    and in controller you can inject it like a dependency

    function MyController(myRouteConfig) {
       myRouteConfig.when ...... 
    }
    

    working example in fiddle