将$ location注入Angular配置会导致“Unknown Provider”错误

时间:2014-03-06 18:05:10

标签: angularjs coffeescript

这是我第一次尝试将Angular与Wordpress一起使用。我想将$location注入config模块,以便我可以像这样使用它:

app = angular.module 'app', [ 'ngResource', 'ngRoute' ]
app.config [ '$routeProvider', '$location', ( $routeProvider, $location )->

    $location.hasPrefix '!'
    $location.html5Mode true

]

不幸的是,将$location$locationProviderconfig一起使用会导致未知的提供商错误。我已经包含了所有必要的依赖关系,即。 angular-route.min.js但是,它仍然无法正常解决。

如果我删除了$location,那就可以了。

app = angular.module 'app', [ 'ngResource', 'ngRoute' ]
app.config [ '$routeProvider', ( $routeProvider )->

    # ROUTES 

]

修改

如果我将$location替换为$locationProvider,我会Failed to instantiate module app due to: TypeError: Object # has no method 'hasPrefix'

2 个答案:

答案 0 :(得分:1)

如果您使用名称$locationProvider作为注射剂的名称,似乎没问题。

Here is a plunker

和js:

angular.module('myApp', ['ngResource', 'ngRoute'])
.config(function($routeProvider, $locationProvider) { // provider-injector
    $locationProvider.hasPrefix = '!';
    $locationProvider.html5Mode = true;
  });

供Docs参考:

  

配置块 - 在提供商注册期间执行   和配置阶段。只能注入提供者和常量   到配置块。这是为了防止意外实例化   服务完全配置之前的服务。

答案 1 :(得分:0)

错误是一个简单的错字。

$location.hasPrefix('!');

应该是

$location.hashPrefix('!');