AngularJs / .provider /如何让rootScope使广播? [英] AngularJs/ .provider / how to get the rootScope to make a broadcast?

查看:189
本文介绍了AngularJs / .provider /如何让rootScope使广播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的任务是重写$ exceptionHandler的供应商,使其输出模式对话框与消息,并停止默认事件。

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

我做的:

在项目中的init我用的方法.provider:

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 :沙盒 - http://jsfiddle.net/STEVER/PYpdM/

推荐答案

您可以注入进样器和查找的$ rootScope。

You can inject the injector and lookup the $rootScope.

示范plunkr:<一href=\"http://plnkr.co/edit/0hpTkXx5WkvKN3Wn5EmY?p=$p$pview\">http://plnkr.co/edit/0hpTkXx5WkvKN3Wn5EmY?p=$p$pview

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

更新:添加.provider技术太:

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

这篇关于AngularJs / .provider /如何让rootScope使广播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆