Ionic how to use event.preventDefault

i see
but could not stop
What should I do?


No one can give me a hint

First you need to pass in the event from your view, like this:

<button (click)="myFunction($event, myParam)">Click Me</button

Then you can call event.preventDefault();

myFunction(event: Event, myParam: any){

1 Like

i try it.but not stop

I already know how to solve it…i use event.stopPropagation();


@beetz12 What about ionic events? like ionChange?

Not sure if I understand the question. What about ionic events?

ionic events, like ‘ionChange’, not support function ‘preventDefault’.
For example:
I use ion-segments buttons.
When change segment, I want to confirm that user doesn’t have un-saved changes at current segment.
If he has, I display confirm-message, and if user answer ‘not continue’, I want to cancel the segment changing.
I didn’t find any way to do that (except the option to manage the segment by manual-code, with not use ngModel at HTML).

stopPropagation() seems to work, preventDefault() NOT

1 Like

Did u get any success? I am also stuck.

The tap event has a property srcEvent that has a method stopPropgation. Call that.

<foo (tap)="doTheThing($event)"></foo>
doTheThing(event) {

ok i will try that. Thanks

I tried event.stopPropagation() and also event.preventDefault(). It does not work for ionic events like ionSlideNextStart, ionSlideWillChange. Are you suggesting event.srcEvent.stopPropagation() or event.ionSlideNextStart.stopPropagation()?

I tried both. Does not work. Based on some condition (unanswered question), in ionSlideNextStart i need to stop the pager from going to next slide.

Please advise!

I tried this and it doesn’t work. :frowning: