2015-10-19 34 views
2

我想设置$ compileProvider.debugInfoEnabled(false);除非将一个查询参数的?debug = true传递给了url。但是$ location在app.config函数中不起作用。Angular.js如何在app.config中使用location.search?

myApp.config(['$compileProvider', '$location', 
function ($compileProvider, $location) { 

    if (!$location.search().debug || $location.search().debug === false) { 
     $compileProvider.debugInfoEnabled(false); 
    } 
} 
]); 

我得到一个错误 '错误:[$注射器:unpr]未知提供商:$位置'

任何想法,这可怎么办呢?

回答

3

形式的documentation

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

$位置是一个服务,并且没有提供。直接访问底层浏览器的位置对象的唯一方法是直接进入。您可以使用$窗口,以帮助单元测试,如果你需要:

if (!$window.location.search.debug || $window.location.search.debug === false) { 
    $compileProvider.debugInfoEnabled(false); 
} 
0

您只能将提供者和常量注入到配置块中。而不是在配置中使用$location尝试在.run()中使用它,或者使用$locationProvider代替。

+1

$ locationProvider不授予访问到浏览器的地址对象 - https://docs.angularjs.org/api/ng/provider/$ locationProvider – bbrown

+0

是@bbrown你是对的。我的意思,而不是你想$ locationProvider:D – maddygoround