5

I would have thought finding this information would be simple with all the work that has gone into Angular2 but apparently not...

1) How can I get the full url of the activated route in Angular2? I know I can call window.location.href but was wondering if there was an Angular way.

2) Is there a way to "build out" a url? If I know when route I want and the parameters for said route, can I construct the full url?

Jason
  • 2,806
  • 2
  • 28
  • 38
  • 2
    The Plunker in http://stackoverflow.com/questions/37517183/how-do-i-get-the-absolute-path-of-the-current-page-in-angular-2/37517504#37517504 provides my attempts. – Günter Zöchbauer Nov 21 '16 at 16:29
  • When the plunker snippet runs, you see "loading..." and then nothing. However, before I posted my question I saw your response during my searching but when I tried location.prepareExternalUrl(location.path()) I was only given back an absolute path...not the *full* path (full path should include protocol, host, port, etc.) – Jason Nov 21 '16 at 18:46
  • Open the browser console. There you get the output for all examples. Worked for me 2h ago. – Günter Zöchbauer Nov 21 '16 at 18:52
  • 1
    The output being shown is `/xvwySO0pwRXUn9CUXcJu?p=preview`. As I already stated, `location.prepareExternalUrl(location.path())` is **not** showing the **full** url. For the full url, I would expect the ouput from your plunker example to be `https://plnkr.co/edit/xvwySO0pwRXUn9CUXcJu?p=preview` – Jason Nov 21 '16 at 19:18
  • This question was not well written. My intent was more complex then what was stated. My apologies. I would vote myself down but it seems you cannot do that on SO. – Jason Dec 13 '17 at 21:09

2 Answers2

0

If you build the url and wanna navigate you can use navigateByUrl method

router.navigateByUrl("/team/33/user/11");
// Navigate without updating the URL
router.navigateByUrl("/team/33/user/11", { skipLocationChange: true});

https://angular.io/docs/ts/latest/api/router/index/Router-class.html#!#navigateByUrl-anchor

Sumant
  • 276
  • 1
  • 12
  • Sorry for delayed response Sumant -- I should have been clearer in that I did not want to do string concatenation. My question was more around "Is there a method I can call with parameters that will build out the url". That being said, I will give you credit since you answered the question as written. – Jason Dec 13 '17 at 21:07
0

You can get the full URL in Angular 2 with the help of document.location. It refers to the current URL. Additionally, document.location is equivalent to window.location.href.

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
Sathish Kotha
  • 1,081
  • 3
  • 17
  • 30