如何使 AngularJS 指令停止传播? [英] How can I make an AngularJS directive to stopPropagation?

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

问题描述

我正在尝试停止传播"以防止在单击 li 中的元素(链接)时关闭 Twitter Bootstrap 导航栏下拉列表.使用这种方法似乎是常见的解决方案.

在 Angular 中,似乎指令是执行此操作的地方?所以我有:

//点击时不关闭下拉菜单directives.directive('stopPropagation', function () {返回 {链接:功能(榆树){$(elm).click(函数(事件){event.stopPropagation();});}};});

...但该方法不属于元素:

TypeError: Object [object Object] 没有方法 'stopPropagation'

我将指令与

联系起来

  • <div>{{foo.text}}<a stop-propagation ng-click="doThing($index)">clickme</a>
  • 有什么建议吗?

    解决方案

    目前,一些指令(即 ng:click)会停止事件传播.这阻止了与依赖于捕获此类事件的其他框架的互操作性."

    em> - 链接

    ...并且能够在没有指令的情况下进行修复,只需执行:

    <a ng-click="doThing($index); $event.stopPropagation();">x</a>

    I am trying to "stopPropagation" to prevent a Twitter Bootstrap navbar dropdown from closing when an element (link) inside an li is clicked. Using this method seems to be the common solution.

    In Angular, seems like a directive is the place to do this? So I have:

    // do not close dropdown on click
    directives.directive('stopPropagation', function () {
        return {
            link:function (elm) {            
                $(elm).click(function (event) {                
                    event.stopPropagation();
                });
            }
        };
    });
    

    ... but the method does not belong to element:

    TypeError: Object [object Object] has no method 'stopPropagation'
    

    I tie in the directive with

    <li ng-repeat="foo in bar">
      <div>
        {{foo.text}}<a stop-propagation ng-click="doThing($index)">clickme</a>
      </div>
    </li>
    

    Any suggestions?

    解决方案

    "Currently some directives (i.e. ng:click) stops event propagation. This prevents interoperability with other frameworks that rely on capturing such events." - link

    ... and was able to fix without a directive, and simply doing:

    <a ng-click="doThing($index); $event.stopPropagation();">x</a>
    

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

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