Monday, November 30, 2015

Exception handling in Angular using $exceptionHandler

Handling uncaught exception

Edit in plnkr






















Steps involved
  • Use $provide service in config section as shown below
Note:We need to customize the handler action
In the example shown below $scope is not defined. This exception is handled and alert is shown to the user


var myAppModule = angular.module('myApp', []);
        myAppModule
        .config(function ($provide) {
            $provide.decorator("$exceptionHandler",
                ["$delegate",
                    function ($delegate) {
                        return function (exception, cause) {
                          
                              $delegate(exception, cause);
                              alert(exception.message);
                        };
                    }]);
        })
        .controller('Details', function () {
            $scope.FirstName = "Prathap";
        });

Output



No comments:

Post a Comment