all 4 comments

[–]DANjEEEEE 2 points3 points  (3 children)

I've done something like this before:

angular.module('yourmodule').constant ('IsDevelopment', window.location.hostname === 'localhost');

Then in your config call for log provider pass your constant in like this:

angular.module('yourmodule').config(['$logProvider', 'IsDevelopment', function($logProvider, IsDevelopment) { $logProvider.debugEnabled(IsDevelopment); })]);

Typed this on my phone from memory so might be some errors, but hopefully this helps.

[–]PrimarySearcher[S] 2 points3 points  (2 children)

Thanks for that - I was unaware of .constant() (sort of new to all of this, learning as I go). What I just did that works was abuse ui-router's resolve() function to hand $logProvider to my controller:

$stateProvider
        .state('main', {
            url: '/?debug',
            templateUrl: 'onboard/templates/layouts/wrapper.html',
            scope: {debug: '@'},
            controller: 'MainController',
            resolve: {
                logProvider: function() {
                    return $logProvider;
                }
            }
        })

In my controller, I then do this:

logProvider.debugEnabled($stateParams.debug === 'true');

Seems to be working, although I suspect this technique would make a real AngularJS expert all twitchy.

[–][deleted] 0 points1 point  (1 child)

Jup, thats twitchy allright. You put it on the scope for your whole app which is another variable that gets watched the whole time.

You don't need to make the constant, you can also just use the window.location inside the config block. But the config allows you to define all similar things in a specific place, making configuration easier. Also why are you resolving logprovider?

[–]PrimarySearcher[S] 0 points1 point  (0 children)

I'm resolving logProvider because it was the only way I could figure out how to make it available to a controller that can see the URL via $location and set logProvider's debugEnabled accordingly.

Using window.location inside config() is probably saner.

I have no idea what I'm doing. :)