Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js How do I use location.search in app.config?

Tags:

angularjs

I am trying to set the $compileProvider.debugInfoEnabled(false); except if a query param of ?debug=true is passed into the url. However $location does not work in the app.config function.

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

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

I get an error 'Error: [$injector:unpr] Unknown provider: $location'

Any idea how this can be done?

like image 699
ryanmc Avatar asked Mar 16 '26 09:03

ryanmc


1 Answers

Form the 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.

$location is a service, and has no provider. The only way to get to the underlying browser's location object is directly. You can use $window to help with unit testing, if you need that:

if (!$window.location.search.debug || $window.location.search.debug === false) {
    $compileProvider.debugInfoEnabled(false);
}
like image 172
bbrown Avatar answered Mar 19 '26 02:03

bbrown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!