Maël Nison
Maël Nison

Reputation: 7353

How do I access the $locationProvider to configure it?

What is the correct way to get the $locationProvider configuration parameters from a service / controller ? When doing a simple dependency injection with function ( $locationProvider ), I get the following error :

Unknown Provider : $locationProviderProvider <- $locationProvider <- myCtrl

Upvotes: 11

Views: 10822

Answers (1)

Henrik N
Henrik N

Reputation: 16294

I also got this error.

You're allowed to inject a $location into a controller, but not a $locationProvider.

Instead, the $locationProvider can be injected into a config method:

var app = angular.module("myApp", []);

app.config(function($locationProvider) {
  $locationProvider.html5Mode(true);
});

app.controller("myCtrl", function($location) {
  $location.path("/some/path");
});

And since I made this additional mistake: it's not just that you should add an app.config bit, but also remember to remove $locationProvider from the controller arguments, or you'll keep getting this error.

If I understand things correctly, provider configuration happens during the configuration phase of the app lifecycle, as opposed to the run phase. Thus this separation. You can read a bit more about these phases here.

I suspect that the reason for the error message is that when you inject $foo into a controller, it looks for a $fooProvider. Thus when we injected a $locationProvider, it looked for a $locationProviderProvider.

Upvotes: 31

Related Questions