AngularJS $location change promise

144 Views Asked by At

I am using angularJS $location service for routing change, the problem is $location.url(someUrl) is asynchronous but doesn't return promise.

So I searched for some solution for this and found these events:

  1. $scope.$on('$locationChangeSuccess')
  2. $routeChangeStart
  3. $routeChangeSuccess

but these events not happening for example when the URL provided to $location.url function is the same as current, I'm seeking for some robust solution to be ensured that $location.url has finished it's work

1

There are 1 best solutions below

1
georgeawg On

Use the $timeout service:

$location.url(someUrl);

var promise = $timeout();

The promise will resolve after the framework has collected all the calls to the $location service and the browser has rendered the new url.

For more information, see