0

I'm trying to redirect back to the previous page using $timeout and $window.history.back(). So when form is submitted a message will show saying (thank you... bla bla) all good from here, but when redirecting back the $timeout doesn't seems to kick in.

<div class="thankyou" data-ng-show="vm.hideEnquiryForm">
            <h3><i class="fa fa-envelope fa-x2"></i> Thank you for enquiring, we will get back to you as soon as possible.</h3>
                <a href="javascript:history.back()">Return</a>
        </div>

vm.submitForm = function () {
        enquireService.postEnquireForm(vm)
            .then(function (response) {
                //$location.path(vm.returnUrl);
                //console.log($location.path() + ' '+ vm.returnUrl);

                if (!vm.hideEnquiryForm) {
                    vm.hideEnquiryForm = true;
                }
                $timeout(function () {
                   $window.history.back;
                }, 3000);
            })
    }
MrNew
  • 1,384
  • 4
  • 21
  • 43

1 Answers1

0

The are many way to do what you want...

The best way, in my opinion, could be using the interface exposed by your router (assuming that you are using a router)...

this is a useful post based on top of UI.Router Angular - ui-router get previous state, so, you need just to go to the previous state using $state.go;

If you cannot do something like that, the only way is using the real history object...

vm.submitForm = function () {
  var returnUrl;
  
  enquireService
    .postEnquireForm(vm)
    .then(function (response) {
      var returnUrl = response.returnUrl;
      
      if (!vm.hideEnquiryForm) {
        vm.hideEnquiryForm = true;
      }
    
      return $timeout(window.history.back.bind(window), 3000);
  });
};
Community
  • 1
  • 1
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • Yeah could but I don't see why injecting a new dependency/plugin is necessary just for this simple redirect back. A window.history will do just fine for now, unless there is a big drawback using window.hisotry.back. – MrNew Dec 08 '15 at 13:04