2015-10-19 81 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