如何访问$ locationProvider进行配置? [英] How do I access the $locationProvider to configure it?

查看:139
本文介绍了如何访问$ locationProvider进行配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从服务/控制器获取$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆