简体   繁体   English

单击子组件而不触发父组件上的clicked事件

[英]Clicking on Child components without firing clicked event on parent component

I have an angular based appication (currently v4.4). 我有一个基于角度的贴图(当前为v4.4)。 I have a view where a component displays an instance of itself for a finite number of times and then in the lowest "child" component, there is an svg. 我有一个视图,其中某个组件在有限的时间内显示其自身的实例,然后在最低的“子”组件中显示一个svg。

I want to be able to pickup the lowest component that has been clicked using the (click) event. 我希望能够拾取使用(click)事件单击的最低组件。 I thought that I might be able to setup the z-index to link to the iteration number so that each subsequent child component appears on top of it's parent, so when anywhere in that area is clicked, the correct event is fired. 我以为我可以设置z-index以链接到迭代编号,以便每个后续子组件都出现在其父组件的顶部,因此,当单击该区域中的任何位置时,都会触发正确的事件。

I have made this demo on plunkr. 我已经在plunkr上进行了此演示 You will see that clicking on the svg does not fire the clicked event, but clicking on any other area inside the boxes, causes the clicked event for that box and all it's parents. 您将看到,单击svg不会触发clicked事件,但是单击框内的任何其他区域,将导致该框及其所有父级的clicked事件。

I would like to be able to toggle all boxes abckground colors independantly. 我希望能够独立地切换所有框的背景色。

Here are the components I have: 这是我拥有的组件:

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <my-child [iteration]="0"></my-child>
    </div>
  `,
})
export class App {
  name:string;
  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }
}

@Component({
  selector: 'my-child',
  template: `
    <div class="border" [style.background]="toggle ? 'green' : 'pink'" (click)="clicked()">
    {{iteration}}
      <my-child [iteration]="iteration" *ngIf="iteration < 3"></my-child>
      <object *ngIf="iteration == 3" 
      id="img" width="300" height="300"
      data="http://snapsvg.io/assets/images/logo.svg"></object>
    </div>
  `,
})
export class Child {
  name:string;
  @Input() iteration;
  toggle: boolean;
  constructor() {
    this.name = `Angular! v${VERSION.full}`
    this.toggle = false;
  }
  ngOnInit() {
    ++this.iteration;
  }
  clicked(){
    this.toggle = !this.toggle;
    console.log(this.iteration)
  }
}

UPDATE: 更新:

The current answers answer the first part of this question, but don't deal with the svg click not firing the click event for the component that displays the svg. 当前的答案回答了该问题的第一部分,但是不处理svg click而不触发显示svg的组件的click事件。

I succeed to did it in your plunker. 我成功地做到了。 You have to use event.stopPropagation() at the top of your cliked function, but you also have to add the $event in your click action, like this : 您必须在cliked函数的顶部使用event.stopPropagation(),但还必须在$ click操作中添加$ event,如下所示:

in your html you must have 在您的HTML中,您必须

(click)=clicked($event)

in your.ts add your stopPropagation : 在your.ts中添加您的stopPropagation:

clicked(event) {
    event.stopPropagation();
}

EDIT : The solution for your last layer, using an invisible div on top of the svg element, you might need to adapt the style or position. 编辑:最后一层的解决方案,在svg元素的顶部使用一个不可见的div,您可能需要调整样式或位置。

<div style="width:300px; height:300px; position:absolute; z-index:1000">
</div>
<object *ngIf="iteration == 3" 
      id="img" width="300" height="300"
      data="http://snapsvg.io/assets/images/logo.svg">
</object>

event.stopPropagation(); at the top of the child element's click handler. 在子元素的点击处理程序的顶部。 You may need to add this to other event handlers for mousedup/down ect. 您可能需要将其添加到mousedup / down等其他事件处理程序中。

Also you could try css: #parentElement { pointer-events: none; } 您也可以尝试使用CSS: #parentElement { pointer-events: none; } #parentElement { pointer-events: none; } on the parent, and .childElement { pointer-events: auto; } #parentElement { pointer-events: none; }和父项上的.childElement { pointer-events: auto; } .childElement { pointer-events: auto; } on the child elements; .childElement { pointer-events: auto; }关于子元素;

and add the id and class name to the parent and children respectively. 并将id和class名称分别添加到父级和子级。

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

相关问题 Angular表单提交事件在父组件和子组件之间触发两次 - Angular form submit event firing twice between parent and child components 如何在不触发父级div事件的情况下触发子级div事件? - How to fire child div event without firing parent div event? 为多个子组件或简单的父组件添加事件监听器 - Adding event listener to multiple child components or simple parent component Aura.js Lightning组件:从上级/上级组件触发嵌套/子组件的方法吗? - Aura.js Lightning Components: Firing a nested/child component's methods from the super/parent component? 如何在单击子级时避免在父级元素上触发事件? - How can I avoid an event from firing on a parent element while clicking on the child? 如何在单击子项时停止为父元素触发onclick事件? - How can I stop an onclick event from firing for parent element when child is clicked? 反应如何防止在单击孩子时触发父母的 onclick 事件? - React how to prevent a parent's onclick event from firing when a child is clicked? 单击子锚点时,如何防止父项的 onclick 事件触发? - How do I prevent a parent's onclick event from firing when a child anchor is clicked? 子点击事件触发父鼠标悬停事件 - child click event firing parent mouseover event 停止父事件处理程序表单触发而不编辑所有子事件处理程序 - Stop parent event handler form firing without editing all child event handlers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM