# Event capture "back" button ionic

**URL:** <https://forum.ionicframework.com/t/event-capture-back-button-ionic/26612>\
**Category:** uncategorized\
**Created:** [June 17, 2015, 8:46pm UTC](https://forum.ionicframework.com/t/event-capture-back-button-ionic/26612 "2015-06-17T20:46:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sioesi](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sioesi](https://forum.ionicframework.com/u/sioesi)\
**Post date:** [June 17, 2015, 8:46pm UTC](https://forum.ionicframework.com/t/event-capture-back-button-ionic/26612/1 "2015-06-17T20:46:07Z")

</div>

Hello community, I need to know how to capture the event of ion-nav button-back-button! if anyone knows how I can do it, I would appreciate it !!

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [June 17, 2015, 9:31pm UTC](https://forum.ionicframework.com/t/event-capture-back-button-ionic/26612/2 "2015-06-17T21:31:51Z")

</div>

You could catch a generic navigation event, but there’s no specific event for the back button.

```auto
  $scope.$on("$ionicView.leave", function(){
    console.log("leaving");
  })

```

---

<div class="post-metadata">

**Author:** ![sioesi](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sioesi](https://forum.ionicframework.com/u/sioesi)\
**Post date:** [June 18, 2015, 1:00pm UTC](https://forum.ionicframework.com/t/event-capture-back-button-ionic/26612/3 "2015-06-18T13:00:16Z")

</div>

Hello @mhartington that event and what I have captured, but I need before you return, ask a question, I need something like that friend!

```
$scope.$on("$ionicView.leave", function(event){
event.stopPropagation();
$ionicPopup.show({
     title: 'View',
     subTitle: '',
     content: '¿Are you sure you back?',
     buttons: [
       { text: 'No',
        onTap: function(e){
          
        }
       },
       {
         text: 'Yes',
         type: 'button-positive',
         onTap: function(e) {
              $ionicHistory.goBack();
         }
       },
     ]
    })
})
```

---

<div class="post-metadata">

**Author:** ![sioesi](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sioesi](https://forum.ionicframework.com/u/sioesi)\
**Post date:** [June 18, 2015, 2:12pm UTC](https://forum.ionicframework.com/t/event-capture-back-button-ionic/26612/4 "2015-06-18T14:12:52Z")

</div>

Also try the following:

```
$scope.$on('$ionicView.beforeLeave', function(event){
alert("Before Leaving");
$ionicPopup.show({
   title: 'View',
   subTitle: '',
   content: '¿Esta seguro de no publicar este view?',
   buttons: [
     { text: 'No',
      onTap: function(e){
        
      }
     },
     {
       text: 'Si',
       type: 'button-positive',
       onTap: function(e) {
            $ionicHistory.goBack();
       }
     },
   ]
  })
 });

```

But the alert is sent well in view corresponding. But ionicPopup the `$ionicHistory.goBack()`runs and then the popup is launched. Why is that?

---

<div class="post-metadata">

**Author:** ![brandyshea](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/brandyshea/32/148286_2.png) [@brandyshea](https://forum.ionicframework.com/u/brandyshea)\
**Post date:** [June 29, 2015, 8:16pm UTC](https://forum.ionicframework.com/t/event-capture-back-button-ionic/26612/5 "2015-06-29T20:16:17Z")

</div>

You could override the `ng-click` of the `ionNavBackButton`:

```
<ion-nav-bar ng-controller="MyCtrl">
  <ion-nav-back-button class="button-clear"
    ng-click="myGoBack()">
    <i class="ion-arrow-left-c"></i> Back
  </ion-nav-back-button>
</ion-nav-bar>

```

JS:

```
function MyCtrl($scope, $ionicHistory) {
  $scope.myGoBack = function() {
    // Code to show popup and then go back
    $ionicHistory.goBack();
  };
}

```

Note: you can put this on one view if you only want it to display on a specific view: [Codepen](http://codepen.io/brandyshea/pen/doJQRb?editors=101) with this added to page 2.
