简体   繁体   English

角嵌套多点击指令

[英]Angular Nesting Multiple Click Directives

I have two directives one is contained within a view, the other is applied to the view's container. 我有两个指令,一个指令包含在一个视图内,另一个指令应用于视图的容器。

<div id="content" data-panel='{{ pNav }}' close-content>
    <div class="inner reveal-animation" ng-view>

    </div>
</div>

Markup loaded inside ng-view: 标记加载到ng-view内部:

<div class="inner-content services">
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2 general-content">
            <h1>Our Services</h1>
            <ul class="tab-nav">
                <li><a href="" class="cx" tab-content><span>AB</span> Customer Service</a></li>
                <li><a href="" class="bx" tab-content><span>CD</span> Existing Applciations</a></li>
            </ul>
            <div class="tab-content">

            </div>
        </div>
    </div>
</div>

Application Directives: 应用指令:

  myApp.directive('closeContent',['PanelServ', function(PanelServ){

  return function(scope, element, attrs){

    element.on('click', function(){
     });

  };

}]);

myApp.directive('tabContent',['PanelServ', function(PanelServ){

    return function(scope, element, attrs){
        element.on('click', function () {
            console.log(element);
        });
    };
}]);

When clicking the anchor tags with the tab-content directive, the tab content directive fires and it is immediately followed by the execution of the close-content directive. 当使用tab-content指令单击锚标记时,将触发tab content指令,并立即执行close-content指令。

Is there something I should include within the tab-content directive to prevent the close-content directive from firing when the tab-content directive is executed? 我应该在tab-content指令中包含一些内容,以防止在执行tab-content指令时触发close-content指令吗?

Is this not how angular is designed to interpret these interactions? 这不是设计角度来解释这些相互作用的方式吗? Can someone point me toward a resource to obtain a better understanding of what I'm attempting to accomplish. 有人可以将我指向一种资源以更好地了解我要完成的工作。

Any enlightenment would be greatly appreciated. 任何启发将不胜感激。

This is called event bubbling . 这称为event bubbling The event triggered on the HTML element is first handled on the element itself and then is propagated to the parent elements upon the DOM tree - thus firing the event handlers of the parent elements. 在HTML元素上触发的事件首先在元素本身上处理,然后在DOM树上传播到父元素-从而触发父元素的事件处理程序。

AngularJS has nothing to do with this, it is how Javascript events work. AngularJS与此无关,这是Javascript事件的工作方式。

To stop the propagation in your case use stopPropagation() method on the event object passed into the handler: 要停止传播,请对传递到处理程序中的event对象使用stopPropagation()方法:

myApp.directive('tabContent',['PanelServ', function(PanelServ){
    return function(scope, element, attrs){
        element.on('click', function (event) {
            event.stopPropagation();
            console.log(element);
        });
    };
}]);

This will prevent the execution of the closeContent directive's click handler. 这将阻止执行closeContent指令的click处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM