-1

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

georgeawg
  • 48,608
  • 13
  • 72
  • 95

1 Answers1

0

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

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • how can you know that the route has ended, it could take more than the timeout interval, for example for heave pages with a lot of data to load – Michael Goldenberg Mar 03 '20 at 09:47