0

As we use, $routeProvider in config method, as of my knowledge $route is a provider.

So, I tried adding new routes to $route in my controller:

After configuring and bootstrapping the application, somewhere in my controller, I tried this:

app.controller('MyCtrl',function($scope,$route) {
  $route.routes['/newRoute'] = { template : 'hey, this is dynamically added route' };
});

But this doesn't seem to work. why?

Any Ideas?

Raghavendra
  • 5,281
  • 4
  • 36
  • 51

1 Answers1

0

you can only configure the routes via your ngapp config function only, so you can't just add routes dynamically in runtime. also $route is a service.

sagie
  • 1,744
  • 14
  • 15
  • $route is not a service, it is a provider which is why we have $routeProvider injected into config block. – Raghavendra Nov 20 '14 at 04:44
  • https://docs.angularjs.org/api/ngRoute/service/$route - and i quote from angular documentation: "The $route service". i think you are confusing what is a service and what is a provider. a provider is used to configure a service and not vice versa. – sagie Nov 20 '14 at 04:46
  • Yes, basically factories, services, providers are called as services. $route is created using app.provider function, and that is why we are able to configure $route service using $routeProvider. So Now in my code, I am trying to inject new route into $route dynamically – Raghavendra Nov 20 '14 at 04:51
  • sorry, you can't do that. docs clearly state that you can only define routes via config function. even if you find a hack, in next version it might not work. don't go there. also with single page apps, not sure you can't define all in begining. – sagie Nov 20 '14 at 04:53