David Ipsen
David Ipsen

Reputation: 1533

AngularJs - Determining the next location's route in $locationChangeStart

Recently I found out about the undocumented $locationChangeStart event in AngularJS when trying to determine where in the framework I should be redirecting users to the login page if their session is invalid.

I'm listening for the event using the following:

$scope.$on("$locationChangeStart", function (event, nextLocation, currentLocation) {
   // Logic goes here
});

where nextLocation and previousLocation are absolute urls to locations within the app.

My question is this: Does AngularJS expose the ability to be able to match a url against a route, much like its internal routing engine does to see if an absolute URL matches a defined route?

If not, what would be the best way to detect a route based on an absolute URL?

Upvotes: 4

Views: 7857

Answers (1)

David Ipsen
David Ipsen

Reputation: 1533

I located the relevant source code lines in the AngularJS github project. Here is the function that it uses to match the URL to a route:

Since this functionality is not exposed by Angular I decided to come up with a rudimentary way of matching the URL to a route since the routes I was matching were relatively simple compared to implementing the same route matching that Angular does.

It ain't pretty, but it works..

Upvotes: 3

Related Questions