2016-03-30 10 views
5

Angular 2を使用してエンコードされたすべてのセグメントで完全なURL表現を返す方法はありますか?

Angular 1 Documentationによると、$ location.absUrl();によると、そのまま返します。

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

方法absUrl();このメソッドはゲッターのみです。

残念ながら、私は見つけることができる最も近い場所を注入された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" 
+1

を指定 ルールに従ってエンコードされたすべてのセグメントとリターンの完全なURL表現にとそれから:this.location.prepareExternalUrl(this.location.path())、すべてを返すexcep "http://example.com" –

+0

@RobLouieには、この種のコードの使用例があります。私はあなたがここにいるものを正確に行う必要がありますが、私はどのように「場所」をインポートするのかに問題があります。 –

+1

この正確な使用例の例はわかりませんが、Angular 2のドキュメントに位置情報を注入して使用する例があります:https://angular.io/docs/js/latest/api/router/Location -class.html。 –

答えて

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

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

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

それは私のために働いた。しかし、これを得るための「適切な」方法はありますか?いずれかへのキャストは少しハックです – Arnaud

関連する問題