Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the $locationProvider to configure it?

Tags:

angularjs

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
like image 852
Maël Nison Avatar asked Apr 06 '14 09:04

Maël Nison


People also ask

What is html5Mode in Angularjs?

html5Mode specifies, we catch all urls on the server, similar to the otherwise route in angular, and return the same html as the root domain. But if I then check the $location object from within the run function of angular, it tells me that http://www.site.com is my host and that /archive is my path .


1 Answers

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.

like image 192
Henrik N Avatar answered Nov 01 '22 03:11

Henrik N