角 - 广播,$在指令中被多次调用 [英] Angular - broadcast , $on called multiple times in directive

查看:302
本文介绍了角 - 广播,$在指令中被多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个单页的应用程序,具有角,我有一个需要2个不同的指令之间的通信基本上不具有父子关系。

在指令中,我有2个地方,我需要从不同的功能广播同一事件。而在指令B,都写在监听一个$。

现在,我观​​察到,每当callFirstFunc&安培;它的广播被称为首次,听者将被调用一次。在随后的通话,监听器被调用两次,三次等,它不断增加。

当callFirstFunc已经执行的callSecondFunc被调用,所以对于这个听者也称为许多没有。时代的听众在广播callFirstFunc。
那么,为什么听者不叫只有一次,为什么多次?它是循环和增加每一次。

指令答:

  app.directive(firstDir功能($ rootScope){
    返回{
        限制:'E',
        链接:功能(范围,元素,ATTRS){
            //其他一些code
            callFirstFunc();
            VAR callFirstFunc =功能(){
                //其他一些code
                $ rootScope $广播(someEvent);
            }
            callSecondFunc();
            VAR callSecondFunc =功能(){
                //其他一些code
                $ rootScope $广播(someEvent);
            }
        }
    };
});

指令A:

  app.directive(secondDir功能($ rootScope){
        返回{
            限制:'E',
            链接:功能(范围,元素,ATTRS){
                //其他一些code
                范围。在(someEvent$,函数(){
                    detectSTuff();
                })
               功能detectStuff(){
                  //其他code
               }
            }
        };
    });


解决方案

我想你忘了解除绑定,甚至处理。

您可以像下面 -

\r
\r

VAR someEventHandle =范围。在$(someEvent功能(){\r
                    detectSTuff();\r
                });\r
范围在$('$毁灭',someEventHandle);

\r

\r
\r

I am working on a single page app, with angular and I have a need to communicate between 2 different directives which basically don't have a parent child relation.

In Directive A, I have 2 places where I need to broadcast same event from different functions. And in Directive B, have written a $on listener for that.

Now, I observe that the whenever callFirstFunc & its broadcast is called for first time, the listener will be called once. Upon subsequent calling, the listener is called twice, thrice and so on ,it keeps on increasing.

The callSecondFunc is called when the callFirstFunc has been executed, so the listener for this is also called as many no. of times the listener for broadcast in callFirstFunc. So, why is the listener not called only once, why multiple times? It is looping and increasing every time.

Directive A:

app.directive("firstDir", function ($rootScope) {
    return {
        restrict: 'E',
        link: function (scope, element, attrs) {
            // some other code
            callFirstFunc();
            var callFirstFunc = function(){
                // some other code
                $rootScope.$broadcast("someEvent");
            }
            callSecondFunc();
            var callSecondFunc = function(){
                // some other code
                $rootScope.$broadcast("someEvent");
            }
        }
    };
});

Directive B:

app.directive("secondDir", function ($rootScope) {
        return {
            restrict: 'E',
            link: function (scope, element, attrs) {
                // some other code
                scope.$on("someEvent", function(){
                    detectSTuff();
                }) 
               function detectStuff(){
                  // other code
               }                    
            }
        };
    });

解决方案

I think you forgot to unbind the even handler.

You can do it like following -

var someEventHandle = scope.$on("someEvent", function(){
                    detectSTuff();
                });
scope.$on('$destroy', someEventHandle);

这篇关于角 - 广播,$在指令中被多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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