6

Is there a way to return the full url representation with all segments encoded using Angular 2?

According to the Angular 1 Documentation, $location.absUrl(); will return it as such.

https://docs.angularjs.org/api/ng/service/$location

Methods absUrl(); This method is getter only.

Return full url representation with all segments encoded according to rules specified in RFC 3986.

// given url http://example.com/#/some/path?foo=bar&baz=xoxo 
var absUrl = $location.absUrl();  
// => "http://example.com/#/some/path?foo=bar&baz=xoxo"
Sevle
  • 3,109
  • 2
  • 19
  • 31
chustedt
  • 61
  • 1
  • 3
  • 1
    Unfortunately the closest I could find was injecting Location and then doing: this.location.prepareExternalUrl(this.location.path()), which returns everything except the "http://example.com" – Rob Louie Mar 30 '16 at 21:04
  • @RobLouie is there an example of this sort of code in use anywhere? I need to do exactly what you have here, but I'm having trouble with how and where to import "location". – Steve Schrab Apr 04 '16 at 17:48
  • 1
    I don't know of an example of this exact use case, but there is an example of injecting and using location in the Angular 2 docs: https://angular.io/docs/js/latest/api/router/Location-class.html. – Rob Louie Apr 04 '16 at 19:25

2 Answers2

5
import {LocationStrategy} from '@angular/common';

constructor(private location:LocationStrategy) {
  var fullPath = (<any>this.location)._platformLocation.location.href;
}
  • 1
    That worked for me. But is there a 'proper' way of getting this? The Cast to any is a little bit hack – Arnaud Dec 29 '17 at 07:32
-1
<script src="https://code.angularjs.org/2.0.0-beta.9/router.dev.js"></script>


import {LocationStrategy} from  '@angular/common'
// pre RC.2 import {LocationStrategy} from  'angular2/router'

constructor(private location:LocationStrategy) {
  this.location.prepareExternalUrl('http://example.com/#/some/path?foo=bar&baz=xoxo');
}

Plunker example

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567