简体   繁体   English

停止传播在角度 8 中没有按预期工作

[英]stoppropagation not working as expected in angular 8

Here is the sample html layout of my component:这是我的组件的示例 html 布局:

<div class="row align-content-start m-0">
  <div *ngFor="let child of list$ | async" class="col-12 mb-3" (click)="clickFunction(child)">
    <div class="d-flex align-items-center">
      <div class="flex-grow-1 border-left px-2 h-100 py-2">
       NAME
      </div>
      <div class="d-flex" (click)="selectDefault($event,child)" *ngIf="isEdit && child.isChecked">
    <i id="company_default" class="material-icons" [ngClass]="child.isDefault ? 'icon-success':''">flag</i>
  </div>
  <div class="custom-control custom-checkbox" *ngIf="isEdit">
    <input name="{{child.name}}" id="id_{{child.name}}" type="checkbox"
      class="custom-control-input" [(ngModel)]="child.isChecked" (change)="checkBoxSelection($event)" />
    <label class="custom-control-label" for="id_{{child.name}}">
    </label>
  </div>
</div>

Now as you can see there are three functions which are called based on events.现在您可以看到,有三个基于事件调用的函数。 selectDefault() it's working fine but if I choose a checkbox the checkBoxSelection() and clickFunction() are both called. selectDefault()它工作正常,但如果我选择一个复选框, checkBoxSelection()clickFunction()都会被调用。 On selecting a checkbox I don't want to trigger the clickFunction() .在选择复选框时,我不想触发clickFunction() Here is the check box function:这是复选框功能:

checkBoxSelection(event: Event) {
  event.stopPropagation();
 }

Please help me to find out the issue / solution.请帮我找出问题/解决方案。

I think that you have a problem because of two different events - click and change .我认为您有问题是因为两个不同的事件 - clickchange

Take a look here Typescript 3 Angular 7 StopPropagation and PreventDefault not working在这里看一下Typescript 3 Angular 7 StopPropagation 和 preventDefault 不起作用

Hope that this will help!希望这会有所帮助!

two events are happening in the background that have no correlation to one another.两个事件在后台发生,彼此没有关联。 1. the change of the checkbox model 2. the click on the checkbox 1.复选框模型的改变 2.复选框的点击

you can try to do something like this你可以尝试做这样的事情

<input name="{{child.name}}" id="id_{{child.name}}" type="checkbox"
      class="custom-control-input" [(ngModel)]="child.isChecked" (change)="checkBoxSelection($event)" (click)="$event.stopPropagation()"  />

Hope this helps!希望这可以帮助!

Thanks everyone for your guidance.谢谢大家的指导。 This resolve my issue.这解决了我的问题。

<div class="custom-control custom-checkbox" *ngIf="isEdit" (click)="$event.stopPropagation()">
  <<<<checkbox here >>>>>>>
</div>

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

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