如何在更改mat-checkbox时停止在调用函数时的事件传播?

blu*_*oud 3 onchange event-propagation

<mat-checkbox (change)="handleProductClick(children, $event)"  [(ngModel)] = "children.selected"
[name]="children.grpId" [id]="children.id"></mat-checkbox>

handleProductClick(selectedProd : Product, event: any)
{
  event.stopPropagation();
}
Run Code Online (Sandbox Code Playgroud)

如果我使用单击功能而不是更改,则可以正常工作。虽然我无法使用click。我必须坚持改变。有没有办法从change函数调用stopPropagation?如果不是,我还能做些什么来阻止事件传播?

blu*_*oud 8

我知道了 必须同时使用复选框和单击复选框。我已经尝试过了。唯一的区别是我在click方法中调用了一个函数,但从未调用过它。如果您在模板中的click方法上调用$ event.stopPropagation,则效果很好。奇怪。以下答案解决了我的问题。Angular 2防止在单击子级时单击父级

<mat-checkbox (change)="handleProductClick(children, $event)"[checked]="children.selected" (click)="$event.stopPropagation()" [name]="children.grpId" [id]="children.id"></mat-checkbox>

  • 这个答案对我来说不起作用,但这是一个很好的轨道。所以我需要做的是: `&lt;mat-checkbox [checked]="element.active" (click)="$event.preventDefault();toggleActive(element)"&gt;&lt;/mat-checkbox&gt;` (2认同)