如何停止AngularJS $广播事件? [英] How to stop $broadcast events in AngularJS?

查看:437
本文介绍了如何停止AngularJS $广播事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个内置的方式,从下降作用域链停止 $广播事件?

Is there a built in way to stop $broadcast events from going down the scope chain?

$广播事件没有一个 stopPropagation 方法(传递的事件对象的的上$ rootScope 提到的文档。)不过的这个合并后的拉申请建议 $广播事件可以有 stopPropagation 呼吁他们。

The event object passed by a $broadcast event does not have a stopPropagation method (as the docs on $rootScope mention.) However this merged pull request suggest that $broadcast events can have stopPropagation called on them.

推荐答案

这angularJS片断1.1.2源$ C ​​$ C:

Snippets from angularJS 1.1.2 source code:

$emit: function(name, args) {
    // ....
    event = {
        name: name,
        targetScope: scope,
        stopPropagation: function() {
            stopPropagation = true;
        },
        preventDefault: function() {
            event.defaultPrevented = true;
        },
        defaultPrevented: false
    },
    // ....
}

$broadcast: function(name, args) {
    // ...
    event = {
        name: name,
        targetScope: target,
        preventDefault: function() {
            event.defaultPrevented = true;
        },
        defaultPrevented: false
    },
    // ...
}

正如你可以看到$广播事件对象不会有stopPropagation。

As you can see event object in $broadcast not have "stopPropagation".

而不是stopPropagation,你可以用preventDefault为了纪念事件为不需要处理这个事件。这不能阻止事件的传播,但是这会告诉孩子们的范围:不需要处理此事件

Instead of stopPropagation you can use preventDefault in order to mark event as "not need to handle this event". This not stop event propagation but this will tell the children scopes: "not need to handle this event"

例如: http://jsfiddle.net/C8EqT/1/

这篇关于如何停止AngularJS $广播事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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