11
<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?

1

1 Answer 1

28

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>

Sign up to request clarification or add additional context in comments.

3 Comments

This took way too long to figure out. Is the (click) property available on all DOM elements by default? Angular Language Service didn't help me find this and Angular Material's documentation didn't have (click) in the API either... I'm not sure why I didn't just try it, but your suggestion saved me from going bonkers.
This answer didn't work for me but was a good track. So what I needed to do in my case was : <mat-checkbox [checked]="element.active" (click)="$event.preventDefault();toggleActive(element)"></mat-checkbox>
No answer seems to work with Angular 15 (neither the accepted one, neither @Célia comment. At the end I figured, that if one has to stop the checkbox from being checked on click, then maybe it isn't the right html element to use for the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.