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
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 .
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With