2

For an app I'm using a skeleton that is very similar to https://github.com/angular/angular-seed.

I've tried something like this, in services.js:

'use strict';

/* Services */

angular.module('mApp.services', []).
  factory('$exceptionHandler', function () {
    return function (exception, cause) {
        alert(exception.message);
    }
});

This doesn't do anything, it doesn't seem to overwrite the exceptionHandler.

If I try:

var mod = angular.module('mApp', []);

mod.factory('$exceptionHandler', function() {
  return function(exception, cause) {
    alert(exception);
  };
});

It overwrite the whole app.

How can I properly overwrite the exceptionHandler if I am using a skeleton similar to the default angular app?

2 Answers 2

2

It's hard to know for certain without seeing the rest of your app, but I'm guessing angular.module('myApp').factory( ... will work. If you leave out the second parameter (,[]) angular will retrieve an existing module for further configuring. If you keep it angular will create a new module.

Sign up to request clarification or add additional context in comments.

2 Comments

You are right, removing [] worked. However is $exceptionHandler a service? In the skel app the services are loaded like this: github.com/angular/angular-seed/blob/master/app/js/services.js
Yes, it's a service provided by angular: docs.angularjs.org/api/ng.$exceptionHandler - you use this service to create your own exception handler (you're not creating a new service)
0

try this example

http://jsfiddle.net/STEVER/PYpdM/

var myApp = angular.module('myApp', ['ng']).provider({

    $exceptionHandler: function(){
        var handler = function(exception, cause) {
            alert(exception);
            //I need rootScope here
        };

        this.$get = function() {
            return handler;
        };
    }
});

myApp.controller('MyCtrl', function($scope, $exceptionHandler) {
    console.log($exceptionHandler);
    throw "Fatal error";
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.