Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop event propagation on calling a function on change of mat-checkbox?

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

handleProductClick(selectedProd : Product, event: any)
{
  event.stopPropagation();
}

If I use click function instead of change it works fine. Although I can't use click. I have to stick with change. Is there a way to call stopPropagation from change function? If not what else can I do to stop the event propagation?

like image 845
blueCloud Avatar asked Aug 29 '18 16:08

blueCloud


People also ask

How do you prevent event propagation?

stopPropagation() Event Method The stopPropagation() method prevents propagation of the same event from being called. Propagation means bubbling up to parent elements or capturing down to child elements.

How do I stop click event propagation?

To stop an event from further propagation in the capturing and bubbling phases, you call the Event. stopPropation() method in the event handler. Note that the event. stopPropagation() method doesn't stop any default behaviors of the element e.g., link click, checkbox checked.

Which prevents propagation on calling event?

The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases. It does not, however, prevent any default behaviors from occurring; for instance, clicks on links are still processed.

When working with events How will you prevent event from being propagated?

stopPropagation(); We can solve this problem by using the stopPropagation() method because this will prevent the parent from accessing the event. Example 1: HTML.


1 Answers

I got it working. Had to use both click and change on the checkbox. I had tried that earlier. The only difference was I was calling a function in the click method and it never got called. If you call $event.stopPropagation on click method in the template, it works well. strange. The below answer solved my problem. Angular 2 prevent click on parent when clicked on child

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

like image 68
blueCloud Avatar answered Nov 01 '22 14:11

blueCloud