简体   繁体   English

Angular 点击事件在另一个点击事件中

[英]Angular click event inside another click event

I have the following setup in my page我的页面中有以下设置

点击内的角度点击

When I click the main box for a division it becomes selected then the department and teams are updated in the tabs on the right.当我单击一个部门的主框时,它会被选中,然后部门和团队会在右侧的选项卡中更新。 However I also want to click the edit icon to edit the name of the division.但是,我还想单击编辑图标来编辑部门的名称。

Because the edit click event is inside the select division click event both events are being triggered when I click on edit.因为编辑点击事件在 select 分割点击事件中,所以当我点击编辑时,这两个事件都会被触发。

What would be the solution to this?解决方案是什么? How do I click the edit button and only trigger that event without triggering the select division event?如何单击编辑按钮并仅触发该事件而不触发 select 除法事件? Do I have to move it outside the html then relative position it?我必须将它移到 html 之外然后相对 position 吗? Is there a better way?有没有更好的办法?

<div class="divisionList">
   <div *ngFor="let division of filteredDivisions" (click)="selectDivision(division)"
       <form [formGroup]="formDivision" fxLayout="row" class="divisionForm">
           <h4 *ngIf="!isDivisionNameBeingEdited(division)">{{division.name}}</h4>
              <input matInput #editTitle (change)="submit()"
                  *ngIf="isDivisionNameBeingEdited(division)" class="mrmd titleInput"
                    id="title2" formControlName="division" />
            <div fxFlex></div>
            <mat-icon (click)="editDivisionName(division)">edit</mat-icon>
        </form>
    </div>
</div>
                                   

This the way click events are handled in JavaScript.这是在 JavaScript 中处理点击事件的方式。 They 'bubble' or propagate up through the parent elements.它们“冒泡”或通过父元素向上传播。 You'll need to handle the event and explicitly tell it to not propagate up the chain of elements.您需要处理该事件并明确告诉它不要向上传播元素链。

<div class="divisionList">
   <div *ngFor="let division of filteredDivisions" (click)="selectDivision(division)"
       <form [formGroup]="formDivision" fxLayout="row" class="divisionForm">
           <h4 *ngIf="!isDivisionNameBeingEdited(division)">{{division.name}}</h4>
              <input matInput #editTitle (change)="submit()"
                  *ngIf="isDivisionNameBeingEdited(division)" class="mrmd titleInput"
                    id="title2" formControlName="division" />
            <div fxFlex></div>
            <mat-icon (click)="editDivisionName($event, division)">edit</mat-icon>
        </form>
    </div>
</div>

in .ts file.ts文件中

editDivisionName($event: MouseEvent, division) {
    $event.stopPropogation();
}

StackBlitz demonstration StackBlitz 演示

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

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