How to access server side configuration in $log decorator

52 views
Skip to first unread message

Daniel Reznick

unread,
Apr 13, 2014, 3:41:44 AM4/13/14
to ang...@googlegroups.com
Hi,

I'm using the decorator to decorate the $log and the $exceptionHandler services and I want to set the logger level and some other stuff in the log decorator base on configuration that I retrieve from the server,

The configuration from the server is being retrieved by using the resolve of $routeProvider  

templateUrl: 'pages/home.html',
    controller: 'HomeCtrl',
    resolve: {
        myConfig: function (appConfig) {
            return appConfig.retreiveConfig();
        }
    }



I tried to $rootScope.$broadcast an event when I retrieve the config from the server and use  $injector.get('$rootScope'); inside the log decorator , like this

        var $rootScope = $injector.get('$rootScope');
        $rootScope.$on('app-config-loaded', function (event, one, two) {
            console.log('heard something...');
        });

But I'm getting Uncaught Error: [$injector:cdep] Circular dependency found: $log <- $exceptionHandler <- $rootScope


How can I use the configuration loaded in the routing resolve inside my $log decorator ?

Gordon Bockus

unread,
Apr 15, 2014, 12:24:38 AM4/15/14
to ang...@googlegroups.com
Hum...I had to look around quite a bit to find a solution that would work for this.  I think you need to first create a module to hold you configuration data.  Check out his article for an example of what that would look like:

It uses grunt, but you could start by just hard coding your config in a module.  There are other less elegant solutions using globals.  Check out this thread: https://groups.google.com/forum/#!topic/angular/B1pRpV-_ig4


Then the simplest solution I could come up with for having your decorator work as expected would be to  move it to a .run for you application injecting the configuration module you created.

So the heart of the answer is don't load the configuration asynchronously.  Instead load it directly before you app is bootstrapped so you have the data when you need it for use in a decorator.   

Gordon

Daniel Reznick

unread,
Apr 16, 2014, 8:48:55 AM4/16/14
to ang...@googlegroups.com
Thanks, I appreciate it, will try it.
Reply all
Reply to author
Forward
0 new messages