是否有Angular 2等同于Angular1的$ location.absURL()?

时间:2016-03-30 20:42:09

标签: angularjs angular

有没有办法返回使用Angular 2编码的所有段的完整网址表示?

根据Angular 1文档,$ location.absUrl();将会这样返回。

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

  

方法absUrl();这个方法只是getter。

     

返回完整的url表示,其中所有段都按照编码   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"

2 个答案:

答案 0 :(得分:5)

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

constructor(private location:LocationStrategy) {
  var fullPath = (<any>this.location)._platformLocation.location.href;
}

答案 1 :(得分: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