简体   繁体   English

如果调用了“ event.stopPropagation()”,则“ @Output”装饰器不会发出事件

[英]“@Output” decorator doesn't emit event if “event.stopPropagation()” called

I have one main component and one child 我有一个主要组成部分和一个孩子

<main>
     <child 
        [nodes]="nodes" 
        (nodeChanged)="someFunction(output)">
     </child>
<main>

Child component renders multilevel tree structure recursively (ie it renders itself if there are children) in which every node is clickable, irrespective of it is terminal node or not. 子组件以递归方式呈现多级树结构(即,如果有子代,则呈现自身),每个节点均可单击,无论它是否为终端节点。

In child component every node has a click event registered "clickedNode()". 在子组件中,每个节点都有一个注册为“ clickedNode()”的点击事件。 Now because I don't want my click event to bubble up to clicked node's head I wrote "event.stopPropagation()" in "clickedNode()" function. 现在,因为我不希望我的click事件冒泡到被单击的节点的头部,所以在“ clickedNode()”函数中编写了“ event.stopPropagation()”。

@Output() nodeChanged = new EventEmitter<Object>();

clickedNode(selectedNode, e:MouseEvent){
    this.nodeChanged.emit(selectedNode);
    e.stopPropagation();
}

Now the problem is @output emitter doesn't if inner level of component gets clicked. 现在的问题是,如果单击组件的内部级别,则@output emitter不会出现。 It emits only if level one nodes are clicked. 仅在单击一级节点时发出。

You can implement two click method for it.One is for stopPrapogation and another one is for EventEmitter 您可以为其实现两种click方法。一种是stopPrapogation ,另一种是EventEmitter

HTML 的HTML

<div (click)="$event.stopPropagation()" (click)="clickedNode(<data you want to pass>)"> 
</div>

TS TS

@Output() nodeChanged = new EventEmitter<Object>();

clickedNode(selectedNode){
    this.nodeChanged.emit(selectedNode);
}

I hope it helps you out. 希望对您有所帮助。

The main thing is that in the HTML you are passing one value and this function expects to receive two values. 最主要的是,在HTML中,您传递了一个值,并且此函数希望接收两个值。 If you want to access to the node clicked you can do it through the event by itself 如果要访问单击的节点,则可以通过事件本身来完成

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

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