How can I return to the previous page with Ionic directives?

I have a normal button with an icon and i want to give to it the feature of a ‘back button’, i.e. I want that when I click on it, the website returns to the previus page. I tried to inject native navigator backHistory() in an Angular function but it doesn’t work.

I tried the code:

document.addEventListener(“backbutton”, function (e) {
navigator.app.backHistory();
}, false);

Below header and button:

<ion-header-bar class="bar-positive">
<button class="button icon ion-arrow-left-c"></button>
<h1 class="title">Detail</h1>
</ion-header-bar>

Any ideas?

Please close this post. I solved my issue using < ion-nav-back-button> and delegate.

  TEMPLATE.HTML
  <ion-nav-bar class="bar-positive">
  <ion-nav-back-button class="button-icon ion-arrow-left-c">
  </ion-nav-back-button>
  </ion-nav-bar>
  
   CONTROLLER.JS

  $scope.goBack(){
  $ionicNavBarDelegate.back();
  }

function goBack() {
window.history.back();
}

This solution is not working anymore (or at least on my experience), try this code instead :

$scope.goBack = function() {
        console.log('Going back');
        $ionicHistory.backView().go();
    };
2 Likes