0

New to AngularJS but slowly getting going with it and so far I like it. I'm coming from the Java/JSP world so bit of a learning curve!

Anyway, I'm trying to figure out how to send all logging to a server side service.

In my app module config I've overridden the Log implementation and I have this working fine - I have log statements automatically creating simply alerts.

Next step was to send them to the server. I've create a service for this using $resource. I then try to autowire the service into my app module config and this is where I've problems.

It's giving me a circular dependency error which I'm not sure what it means or how to resolve it.

Anyone done anything similar before who may have encountered this problem?

The error I'm seeing is 'Uncaught Error: Circular dependency: $browser <- $httpBackend <- $http <- $resource <- LoggingService <- $log <- $exceptionHandler <- $rootScope'

My app config is:

app.config(['$provide', function($provide) {      

    $provide.decorator('$log', function($delegate, LoggingService) {

        var _info = $delegate.info;
        var _error = $delegate.error;

        $delegate.info = function(msg){
            _info(msg);
        };

        $delegate.error = function(msg){
            _error(msg);
            //log.error(msg);
            alert('Error:' + msg);
        };
        return $delegate;
    });

}]);

Just trying to pass in my LoggingService results in the error.

My logging service is very simple:

app.factory('LoggingService', ['$resource', function($resource) {
return $resource('http://localhost:port/myservice/logging/', {port: ':8080'},
    {
        save: {method: 'POST'}
    }
);
}]);

Regards, Kevin.

kevfuzz
  • 247
  • 2
  • 3
  • 14
  • Does it give any more details in the error message? – Joachim Isaksson Dec 05 '13 at 17:22
  • Hi Kevin, Can you add your code for the service in Angular and then the server side stuff you've written to receive this? – Alan H Dec 05 '13 at 17:23
  • Further details added above. – kevfuzz Dec 05 '13 at 17:31
  • 1
    For anyone looking at this I've set up a Plunkr that replicates the issue: http://plnkr.co/edit/onQGUqHOzUst0Fa3YK7C?p=preview – Alan H Dec 05 '13 at 17:51
  • Thanks Alan, I'm still only get used to this more Web centric style development and using sites like plunker for help. – kevfuzz Dec 05 '13 at 17:59
  • Your service works fine when passed via a controller. I think passing it to $provide creates the circular dependency on $http but I'm not sure why. – Alan H Dec 05 '13 at 19:22
  • This looks pretty relevant: http://stackoverflow.com/questions/14681654/i-need-two-instances-of-angularjs-http-service-or-what – Alan H Dec 05 '13 at 19:23

1 Answers1

0

As per Injecting Dependencies in config() modules - AngularJS You can only use providers in .config

See registering a service with $provide https://docs.angularjs.org/guide/services

Community
  • 1
  • 1
t.durden
  • 178
  • 3
  • 15