Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs/ .provider / how to get the rootScope to make a broadcast?

Now my task is to rewrite $exceptionHandler provider so that it will output modal dialog with message and stop default event.

What I do:

in project init I use method .provider:

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

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

})

standart inject method does not work.

UPD: sandbox - http://jsfiddle.net/STEVER/PYpdM/

like image 594
Stepan Suvorov Avatar asked Feb 19 '13 10:02

Stepan Suvorov


1 Answers

You can inject the injector and lookup the $rootScope.

Demo 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);
        }
    };
})

Update: add .provider technique too:

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);  
    }
  };
});
like image 147
checketts Avatar answered Sep 22 '22 07:09

checketts