I have a problem with tap events, that doesn’t happen with click events, and makes it apparently impossible to stop the event bubbling from an inner component (child) to an outer one (parent) (at least without hacks).
To simulate the problem I’ve created a sidemenu starter project:
ionic start --v2 myApp sidemenu
Then I’ve changed the ion-list
inside the app.html
to:
<ion-list>
<ion-item-group>
<ion-item-divider>
Click
</ion-item-divider>
<button menuClose ion-item *ngFor="let p of pages" (click)="onOuterClick($event)">
{{p.title}}
<button item-right clear (click)="onInnerClick($event)">
<ion-icon name="close"></ion-icon>
</button>
</button>
</ion-item-group>
<ion-item-group>
<ion-item-divider>
Tap
</ion-item-divider>
<button menuClose ion-item *ngFor="let p of pages" (tap)="onOuterClick($event)">
{{p.title}}
<button item-right clear (tap)="onInnerClick($event)">
<ion-icon name="close"></ion-icon>
</button>
</button>
</ion-item-group>
</ion-list>
And added the following methods to the app.component.ts
:
onOuterClick(event: Event) {
console.log('onOuterClick');
}
onInnerClick(event: Event) {
if (event && event.stopPropagation) {
console.log('onInnerClick - stopPropagation');
event.stopPropagation();
}
console.log('onInnerClick');
}
Then I run ionic serve
and click both the areas (one at a time) in the menus outside the inner button and the inner button (icon) itself, to simulate the behaviour:
Logs - Click Event - Outer Area:
onOuterClick
Logs - Click Event - Inner Area (Icon):
onInnerClick - stopPropagation
onInnerClick
Logs - Tap Event - Outer Area:
onOuterClick
Logs - Tap Event - Inner Area (Icon):
onOuterClick
onInnerClick
The click events behaves the expected way, stoping the propagation of the event when the inner component is clicked, but the tap events have 2 problems:
-
The event is called first in the outer element and after in the inner one, that is, the bubbling order is inverted.
-
The event doesn’t have a
stopPropagation
method, as opposite to all (or most) normal HTML DOM events, so I can’t stop its propagation.
Is there a way to make the tap event behaves like the click event, concerning the bubble behaviour?