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);
            })
    }
3
  • Need invocation parens on your call to .back Commented Dec 8, 2015 at 12:18
  • You should be getting console errors for the .back you used ? Commented Dec 8, 2015 at 12:22
  • 1
    @JAAulde all good now, I was missing the brackets (). should be $window.history.back(); Commented Dec 8, 2015 at 12:22

1 Answer 1

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);
  });
};

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.