How to auto redirect after X seconds in AngularJS?

前端 未结 5 785
自闭症患者
自闭症患者 2021-01-17 22:24

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
5条回答
  •  迷失自我
    2021-01-17 23:08

    Basically, Their are two way $location and $timeout. This will solve your issue:

    1. By $timeout method :

    code :

      .controller('HomeController', ['$scope', '$state', '$timeout',
                                    function($scope, $state, $timeout) {    
       $timeout(function() {
          $state.go('anotherstate');
          }, 4000);
    
        }])
    
    1. By $location method :

    code:

    .controller('HomeController', ['$scope', '$state', '$location',
                                        function($scope, $state, $location) {    
          $location.path('/appurl');
    
            }])
    

提交回复
热议问题