HP.
HP.

Reputation: 19896

$window.history.back does not work in Angularjs

I am borrowing an example here to implement Back button:

How to implement history.back() in angular.js

The way I did it was to create a function back():

http://jsfiddle.net/qhoc/WaRKv/110/

app.controller('Ctrl', ['$scope', '$window', '$location', function($scope, $window, $location) {

  $scope.log = function() {
    console.log($location.path());
  };

  $scope.back = function() {
    $window.history.back();
    console.log($location.path());
  };

}]);

But it doesn't work. The event is hit but the location output in console log doesn't change.

Please help

UPDATE 12/28:

I ran http://jsfiddle.net/WaRKv/111/ and did the following:

  1. Click Link 1 once
  2. Click Link 2 once
  3. Then finally click Back once.

Please see below screenshot. It doesn't work still as the Back button should log Link 1 but it kept showing Link 2.

enter image description here

Upvotes: 0

Views: 5698

Answers (1)

kfis
kfis

Reputation: 4729

You have to listen for the $locationChangeSuccess Event before logging to the console.

 $scope.$on("$locationChangeSuccess",function(event,newUrl, oldUrl) {
        console.log("location: "+$location.path());
    });

You log the location before it is finished changing.

See:

http://jsfiddle.net/WaRKv/111/

Unfortunately somehow the event is revieved twice, I dont know why and the back button doesnt work as well :/ maybe jsfiddle problems

Upvotes: 1

Related Questions