Ionic how to use event.preventDefault

i see http://learnangular2.com/events/
but could not stop
What should I do?

2 Likes

No one can give me a hint

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

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

Then you can call event.preventDefault();

Controller:
myFunction(event: Event, myParam: any){
event.preventDefault();
}

1 Like

i try it.but not stop

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

7 Likes

@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) {
 event.srcEvent.stopPropagation();
}
3 Likes

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: