0

I'm new to Angular and actually I'm trying to update a codebase with ajax http to angular http ($http). But when I try to add $http as function param I got circular DI error.

app.provider(
    "$exceptionHandler",
    {
        $get: function(errorLogService ) {
            return( errorLogService );
        }
    }
);
    // -------------------------------------------------- //
    // -------------------------------------------------- //
    // The error log service is our wrapper around the core error
    // handling ability of AngularJS. Notice that we pass off to
    // the native "$log" method and then handle our additional
    // server-side logging.
    app.factory(
        "errorLogService",
        function($log, $window, stacktraceService ) { // <-- If I add $http I got an exception (see the Error Log bellow)
            // I log the given error to the remote server.
            function log(exception, cause) {
                // Pass off the error to the default error handler
                // on the AngualrJS logger. This will output the
                // error to the console (and let the application
                // keep running normally for the user).
                $log.error.apply( $log, arguments );
                // Now, we need to try and log the error the server.
                // --
                // NOTE: In production, I have some debouncing
                // logic here to prevent the same client from
                // logging the same error over and over again! All
                // that would do is add noise to the log.
                try {
                    var errorMessage = exception.toString();
                    var stackTrace = stacktraceService.print({ e: exception });
                    // Log the JavaScript error to the server.
                    // --
                    // NOTE: In this demo, the POST URL doesn't
                    // exists and will simply return a 404.
                    /*$.ajax({
                        type: "POST",
                        url: "./javascript-errors",
                        contentType: "application/json",
                        data: angular.toJson({
                            errorUrl: $window.location.href,
                            errorMessage: errorMessage,
                            stackTrace: stackTrace,
                            cause: ( cause || "" )
                        })
                    });*/
                    $http({
                            url: "http://example.appspot.com/rest/app",
                            method: "POST",
                            data: angular.toJson({
                            errorUrl: $window.location.href,
                            errorMessage: errorMessage,
                            stackTrace: stackTrace,
                            cause: ( cause || "" )
                        })
                        }).success(function(data, status, headers, config) {
                            $scope.data = data;
                        }).error(function(data, status, headers, config) {
                            $scope.status = status;
                        });
                } catch ( loggingError ) {
                    // For Developers - log the log-failure.
                    $log.warn( "Error logging failed" );
                    $log.log( loggingError );
                }
            }
            // Return the logging function.
            return( log );
        }
    );

Error Log:

ionic.bundle.js:13438 Uncaught Error: [$injector:cdep] Circular dependency found: $rootScope <- $http <- errorLogService <- $exceptionHandler <- $rootScope
http://errors.angularjs.org/1.5.3/$injector/cdep?p0=%24rootScope%20%3C-%20%…0%3C-%20errorLogService%20%3C-%20%24exceptionHandler%20%3C-%20%24rootScope
4

0 回答 0