0

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

Community
  • 1
  • 1
HP.
  • 19,226
  • 53
  • 154
  • 253
  • 1
    You code works, it just seems like it's not going back because it's loaded in an iframe in jsfiddle. Try it full screen here: http://jsfiddle.net/qhoc/WaRKv/110/embedded/result/ – Hattan Shobokshi Dec 27 '13 at 21:28
  • I just tried that link. Still dont work for me in both Safari and Chrome. The back button kept output the existing url – HP. Dec 27 '13 at 22:13

1 Answers1

1

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

kfis
  • 4,739
  • 22
  • 19