William Falcon
William Falcon

Reputation: 9823

Read url requested in Angular app

When a page loads, I want to determine what url was used to load that page and intercept it in the controller so I can extract the parameters from it.

For example if http://mypage.com/#/myPage/otherStuff

How can I intercept this URL before the page loads?

Thank you.

(Use case) 1. user requests my page 2. I intercept, take out the parameters from the url 3. I format the page to match their parameters.

Upvotes: 1

Views: 159

Answers (2)

m59
m59

Reputation: 43785

It sounds like you're looking for these events (read the docs here).

$scope.$on("$locationChangeStart", function (event, nextLocation, currentLocation) {

});

From reading the comments, I see that your question is an X/Y issue. My answer deals with your question of intercepting the url in the controller, but it looks like your overall goal is better accomplished by other means.

Upvotes: 2

Aaron Torgerson
Aaron Torgerson

Reputation: 992

I would suggest using either ngRoute or ui-router to define your route and make the variable parts of the uri into route parameters, which would then be available in your controller function.

Upvotes: 0

Related Questions