如何访问$ locationProvider进行配置? [英] How do I access the $locationProvider to configure it?
问题描述
从服务/控制器获取$locationProvider
配置参数的正确方法是什么?用function ( $locationProvider )
执行简单的依赖项注入时,出现以下错误:
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
推荐答案
我也遇到了此错误.
您可以将$location
注入控制器,但不能 注入$locationProvider
.
You're allowed to inject a $location
into a controller, but not a $locationProvider
.
相反,可以将$locationProvider
注入到config
方法中:
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");
});
由于我犯了这个附加错误:不仅是您应该添加一个app.config
位,而且还记得从控制器参数中删除$locationProvider
,否则您将不断遇到此错误.
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.
我怀疑错误消息的原因是,当您将$foo
注入控制器时,它会寻找$fooProvider
.因此,当我们注入$locationProvider
时,它会寻找$locationProviderProvider
.
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
.
这篇关于如何访问$ locationProvider进行配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!