本文介绍了我怎样才能让一个AngularJS指令stopPropagation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想stopPropagation,以prevent从被点击的李中的元素(链接)时关闭Twitter的引导导航栏下拉列表。使用这种方法似乎是共同solution.

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'

我配合的指令以

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

有什么建议?

推荐答案

目前一些指令(即NG:点击)停止事件传播这prevents与依赖于捕捉此类事件的其他框架的互操作性。 的 -

......,并能无指令来解决,并简单地做:

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

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

这篇关于我怎样才能让一个AngularJS指令stopPropagation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 05:26
查看更多