18

今私の仕事は $exceptionHandler プロバイダーを書き直して、メッセージ付きのモーダル ダイアログを出力し、デフォルト イベントを停止するようにすることです。

私がやること:

プロジェクトの初期化では、メソッド .provider を使用します。

.provider('$exceptionHandler', function(){

//and here I would like to have rootScope to make event broadcast

})

標準の注入方法は機能しません。

UPD : サンドボックス - http://jsfiddle.net/STEVER/PYpdM/

4

3 に答える 3

31

インジェクターを注入して、$rootScope をルックアップできます。

plunkr のデモ: http://plnkr.co/edit/0hpTkXx5WkvKN3Wn5EmY?p=preview

myApp.factory('$exceptionHandler',function($injector){
    return function(exception, cause){
        var rScope = $injector.get('$rootScope');
        if(rScope){
            rScope.$broadcast('exception',exception, cause);
        }
    };
})

更新: .provider テクニックも追加:

app.provider('$exceptionHandler', function() {
  // In the provider function, you cannot inject any
  // service or factory. This can only be done at the
  // "$get" method.

  this.$get = function($injector) {
    return function(exception,cause){
      var rScope = $injector.get('$rootScope');
      rScope.$broadcast('exception',exception, cause);  
    }
  };
});
于 2013-02-19T16:19:23.030 に答える
1

これを行う私の方法-デコレーターを使用し、不明なエラーで以前の例外ハンドラーに戻ります:

app.config(function ($provide) {
  $provide.decorator('$exceptionHandler', function($delegate, $injector) {
    return function (exception, cause) {
      if (ICanHandleThisError) {
        var rootScope= $injector.get('$rootScope');
        // do something (can use rootScope)
      } else
       $delegate(exception, cause);
    };
  });
});
于 2013-11-02T12:33:15.040 に答える
-2

$rootScope を注入する必要があります。

.provider('$exceptionHandler', '$rootScope', function(){

//and here I would like to have rootScope to make event broadcast

})

これはあなたが試したものですか?その場合、失敗した理由を確認するためのエラー メッセージまたは jsfillde/plnkr がありますか?

于 2013-02-19T10:08:34.497 に答える