5

I am trying to access $routeProvider in one of my controller in order to add a route. How do I do that?

function Cont($scope,$routeProvider) {

};

This doesn't work for me; I am getting: Error: Unknown provider: $routeProviderProvider <- $routeProvider

metamatt
  • 13,809
  • 7
  • 46
  • 56
ciochPep
  • 2,472
  • 4
  • 26
  • 30
  • 3
    This has been previously answered. Take a look at http://stackoverflow.com/a/13173667/1620332 . – jonc Feb 20 '13 at 16:09
  • possible duplicate of [How to defer routes definition in Angular.js?](http://stackoverflow.com/questions/13153121/how-to-defer-routes-definition-in-angular-js) – pkozlowski.opensource Feb 20 '13 at 16:14
  • The useful answer is indeed [How to defer routes definition in Angular.js](http://stackoverflow.com/questions/13153121/how-to-defer-routes-definition-in-angular-js), but the nice thing about this question is you can get here with a web search for the error message. – metamatt Jul 12 '13 at 17:25

2 Answers2

6

$routeProvider and other providers can only be injected to a modules config block. What is it that you want to do with the $routeProvider inside a controller?

Anders Ekdahl
  • 22,685
  • 4
  • 70
  • 59
  • It would be nice to explain *why* providers can only be injected into config blocks (I think this is explained in Module Loading & Dependencies section of the [Module developers' guide](http://docs.angularjs.org/guide/module)). – metamatt Jul 12 '13 at 17:27
1

In the controller, $route is accessible but $routeProvider is not. Maybe you can just copy the function out, for example, the 'when' and 'pathRegExp'

See jsfiddle: http://jsfiddle.net/5FUQa/1/

  function addRoute(path, route) {
     //slightly modified 'when' function in angular-route.js
  }
  addRoute('/dynamic', {
    templateUrl: 'dynamic.tpl.html'
  });

Also see: How to defer routes definition in Angular.js?

Community
  • 1
  • 1
SetupX
  • 413
  • 2
  • 5
  • 13