1

I’ve got an Ionic page with a simple swipe gesture using the GestureController as described in the docs. On the swipe end event I increment a counter. There is a binding on the html of the page, which doesn’t get updated. Console.log() shows the updated counter, though.

Is there a known issue about that or am I doing something wrong?

Page Template

<ion-content #contentElement>
    {{ counter }}
</ion-content>

Typescript

export class TestPage {

  counter = 0;

  swipeGesture: Gesture;
  @ViewChild('contentElement', { static: true, read: ElementRef }) contentElement: ElementRef;

  constructor(private gestureController: GestureController) { }

  ionViewDidEnter() {
    this.swipeGesture = this.gestureController.create({
      el: this.contentElement.nativeElement,
      gestureName: 'swipe',
      onEnd: () => this.onSwipeEnd(),
    });
    this.swipeGesture.enable();
  }

  private onSwipeEnd() {
    this.counter++;
    console.log(this.counter);
  }

}
3
  • Its not giving any output for counter (not output)? Commented Feb 17, 2020 at 0:33
  • Yes it does. There is a debug output in onSwipeEnd(). Commented Feb 17, 2020 at 6:10
  • I have the same problem now. I follow the tutorial of ionic3 angular but it didn't work with current version. Something like: (tap)="tapEvent($event)" Commented Aug 23, 2021 at 4:21

1 Answer 1

1

I had a similar issue and what I did was inject the ChangeDetectorRef into the constructor.

constructor(private ref: ChangeDetectorRef) {}

Then in you can run the change detection manually.

private onSwipeEnd() {
   this.counter++;
   console.log(this.counter);
   this.ref.detectChanges(); // Runs the change detection
}
1
  • No problem! I had the same frustration yesterday.
    – D_Vil
    Commented Feb 17, 2020 at 21:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.