Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button with IonicHistory.goBack()

I have a simple App built with Ionic/Cordova. In each page I want to create a simple "Go Back" block button - the user can press it to go to the previous page in the App.

I'm thinking of doing this using $ionicHistory. However, the method $ionicHistory.goBack() is not working.

I am currently using the usual window.history.back() instead, which works, but I don't understand why the ionic method is not working like it should.

Here is the view of the code:

<button class="button button-block button-assertive" ng-click="goBackHandler()">
   Go Back
</button>

And here is the Controller:

angular.module('starter.controllers', ['ionic'])

.controller('AppCtrl', function($scope, $ionicHistory) 
{
    $scope.goBackHandler = function()
    {
        $ionicHistory.goBack();                      //This doesn't work
        //window.history.back();                          //This works
        //alert('code to go back called. Did it work?');  //For testing
    }
});

This should be pretty straight-forward. What could I be missing?

Edit: Plunker here - http://plnkr.co/yJqdfs

like image 414
PDoria Avatar asked Aug 31 '15 15:08

PDoria


1 Answers

$ionicHistory.goBack() works only when you have routing and when you navigate to another screen using $state.go('testPage').

In the plunker you've

<a href="test.html">Go to Test Page</a>

Which is a general browser redirection, which IonicHistory can't help you.

Ionic History will work based on the states stack it has.

Because when you redirect using href="test.html", there will be NO state pushed to Ionic History, so $ionicHistory.goBack() couldn't find any back state.

like image 79
Raj Kumar Avatar answered Oct 18 '22 17:10

Raj Kumar