Ionic 2: Generated back button click event

tpoker picture tpoker · Aug 11, 2016 · Viewed 45.4k times · Source

I'm trying to log the user's action of clicking the generated back button in the navigation bar, but I can't find a way to handle the click event?

It seems like the ion-nav-back-button from here isn't working anymore?

Answer

AnatolyS picture AnatolyS · Jan 25, 2017

According to the official Ionic docs, you can override the backButtonClick() method of the NavBar component:

import { ViewChild } from '@angular/core';
import { Navbar } from 'ionic-angular';

@Component({
  selector: 'my-page',
  template: `
    <ion-header>
      <ion-navbar>
        <ion-title>
          MyPage
        </ion-title>
      </ion-navbar>
    </ion-header>

    <ion-content>
    ...
    </ion-content>
   `
})
export class MyPage {
  @ViewChild(Navbar) navBar: Navbar;
  constructor(private navController: NavController){}
  ionViewDidLoad() {
    this.navBar.backButtonClick = (e:UIEvent)=>{
     // todo something
     this.navController.pop();
    }
  }
}