Ahmad
Ahmad

Reputation: 53

How to use `$window.history.back()` to go back in Angular.js?

I am using $window.history.back() to go 1 step back in my Ionic + Angular app.

It has an issue: if my URL is in proper form (http://localhost:8100/jobs.html) then it is working properly, but if my URL is in this form (http://localhost:8100/jobs.html#/category) then it is not working and does not go back.

Here is my function:

$scope.back3=function() {    
    $window.history.back(); 
}

I will be thankful for any help.

Upvotes: 2

Views: 16899

Answers (2)

Gajotres
Gajotres

Reputation: 57309

If you want to use Ionic Framework history navigation you should also use proper Ionic Framework history object:

Use this if you want to go to the previous view:

$ionicHistory.goBack();

More about it can be found here: $ionicHistory

Upvotes: 2

Karan Bhutwala
Karan Bhutwala

Reputation: 797

You need to use window.history.go(-1); in $ionicPlatform.registerBackButtonAction event. It will go back on pressing back button key. below is the example :

app.controller('HomeCtrl', function($ionicPlatform){
 $ionicPlatform.registerBackButtonAction(onBackKeyDown, 100);

  function onBackKeyDown() {
       window.history.go(-1);
    }
 }
});

For giving back button action on any button use following code:

HTML

<div ng-click="goBack()">Back</div>

JavaScript

$scope.goBack = function() {
  window.history.back();
};

Upvotes: 3

Related Questions