AngularJs/.provider /如何让rootScope进行广播?

Ste*_*rov 18 javascript provider broadcast angularjs

现在我的任务是重写$ exceptionHandler提供程序,以便它将输出带消息的模态对话框并停止默认事件.

我所做的:

在项目初始化我使用方法.provider:

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

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

})
Run Code Online (Sandbox Code Playgroud)

标准注射方法不起作用.

UPD:沙箱 - http://jsfiddle.net/STEVER/PYpdM/

che*_*tts 31

您可以注入进样器并查找$ rootScope.

演示插件: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);
        }
    };
})
Run Code Online (Sandbox Code Playgroud)

更新:添加.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);  
    }
  };
});
Run Code Online (Sandbox Code Playgroud)