2016-05-19 25 views
2

私は角度のあるアプリケーションがあります。これにはn個のコントローラがあります。私は$ exceptionHandlerについて知っています。これは、カスタムの例外ハンドラです。コントローラangularjsで例外を処理する方法

app.controller('cust1Controller',function($scope,$exceptionHandler) 
{ 
    //ajax request or anything 
} 

app.controller('cust2Controller',function($scope,$exceptionHandler) 
{ 
    //ajax request or anything 
} 

任意の例外は、コントローラのいずれかで発生した場合

'use strict'; 

angular.module('exceptionHandlingApp') 
.config(function($provide) { 
$provide.decorator('$exceptionHandler', ['$log', '$delegate', 
    function($log, $delegate) { 
    return function(exception, cause) { 
     $log.debug('Default exception handler.'); 
     $delegate(exception, cause); 
    }; 
    } 
]); 
}); 

は、どのように私は私のカスタムハンドラを呼び出すのですか?

+0

任意のコントローラに「throw 'error''」と書いてください。カスタムハンドラには行きませんか? –

+0

A [デモはこちら](http://jsfiddle.net/stever/pypdm/); –

+0

@ YinGang、それは私が知っている。 throw文を使用して手動でエラーをスローしています。 –

答えて

0

例外のハンドラを呼び出すべきではありません。これはAngularJSによって自動的に呼び出されます。

というキーワードを使用することができます。私に例を挙げてみましょう。

angular 
    .module("app") 
    .config(logExceptionToAPIServer); 

function logExceptionToAPIServer($provide) { 
    $provide.decorator("$exceptionHandler", ["$delegate", 
     function ($delegate) { 
      return function (exception, cause) { 
       debugger; 
       var http = angular.injector(['ng']).get('$http'); 
       debugger; 
       var request = { 
        Message: exception.message, 
        StackTrace: exception.stack 
       }; 
       http.post('yourAddress//', request); 
       $delegate(exception, cause); 
      }; 
     } 
    ]); 
}; 
関連する問題