Search code examples
angularjstimeoutlocation

$location inside a $timeout in AngularJS


I have a controller in AngularJS, with $timeout and $location in the header. The controller has the following code:

$timeout(function () {
    $location.path("/nextview")}, 500, false)
}

Due to the async nature of the $timeout, it seems that $location isn't passed, and therefore fails. If I put $location as part of the function arguments, it still doesn't work.

$timeout(function ($location) {
    $location.path("/nextview")}, 500, false)
}

However if I have only the below, it works:

    $location.path("/nextview")};

I really need some sort of timeout as the code before the location change is to give the user a visual cue. If the location is changed immediately, there's no visual cue.

Is there a better method to do a pause or sleep, maybe synchronously? Thanks in advance :D


Solution

  • Try doing it like this

    $timeout(function() {
        $scope.$apply(function() {
            $location.path("/nextview");
        });
    }, 2000);
    

    If this doesn't work, check your console for any errors.