Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto redirect after X seconds in AngularJS?

Tags:

angularjs

What is the right way to do this in AngularJS? I didn't find any simple answer to this.

I'd like to :

  1. Load a page
  2. Wait for X seconds
  3. Being automatically redirected to another page after these seconds.

Note: I'm using ui-routing (states) to perform redirections

Thanks.

like image 589
David D. Avatar asked Oct 14 '14 19:10

David D.


2 Answers

This works (thanks PSL):

.controller('SeeYouSoonCtrl', ['$scope', '$state', '$timeout',
                                function($scope, $state, $timeout) {

    $timeout(function() {
      $state.go('AnotherState');
      }, 3000);

    }])
like image 125
David D. Avatar answered Oct 17 '22 01:10

David D.


It does work also using $location instead of $state!

JS

app.controller('SeeYouSoonCtrl', function($scope, $location, $timeout) {

    $timeout(function() {
      $location.path('/nextsite');
      }, 3000);

    });
like image 27
Sven Ya Avatar answered Oct 17 '22 01:10

Sven Ya