Configure $ logprovider based on parameters
I want (by default) to hide hidden instructions
$logProvider.debugEnabled(false);
BUT I want developers to access debug statements through some kind of path parameter (or something like that). We have multiple "Dev" servers and want to quickly see the debug instructions.
What I wanted to do was provide a url like:
myapp.com/mypage.html?debug=true
enable debugging $ locationProvider.
I wanted to do something like:
$urlParams = $location.search();
if($urlParams.debug) {
$logProvider.debugEnabled(true);
}
in config ... but I can't use $ location injection ... is there anyway to do some logic in the config section ... or better yet, is there a way to configure and application on the fly
source to share
So, I was unfortunately unable to get the solution posted above / below using UI.router $ stateProvider.
Here is the solution I came up with:
Create your own provider containing a function to get Params queries based on the solution found here: How to get query string values ββin JavaScript?
app.provider("query",function(){
return{
getParamsByName: function(name){
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
},
$get: function(){ return {} }
};
});
Then in my controller all I have to do is add the queryProvider to the config,
and then call getParamsByName ("debug")
source to share
You can use UI-router
, for example, something like this (coffeescript):
'use strict'
angular.module 'App'
.config ($stateProvider, $urlRouterProvider) ->
$stateProvider
.state 'index',
abstract: true
url: '/'
templateUrl: '<div ui-view></div>'
controller: 'indexController'
resolve:
debugResolved: ($stateParams, $logProvider) ->
if $stateParams.debug == 'true'
$logProvider.debugEnabled(true)
This is just an example, I haven't tried it, but it should work as expected. You can read more about the UI router here: https://github.com/angular-ui/ui-router
source to share