Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current url in the browser in Angular 2 using TypeScript?

I need to get the current URL present in the browser in my Angular 2 application.

In JavaScript normally we do it using the window object.

How can I do this in Angular 2 using TypeScript?

Thanks.

like image 758
Kevin Avatar asked Jun 13 '16 18:06

Kevin


People also ask

How do I get the current URL in my browser?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.


1 Answers

This is late but I thought it was worth updating. As of Angular2 final release you can import DOCUMENT from @angular/common and use that to get to the location.

import { Component, Inject } from '@angular/core'; import { DOCUMENT } from '@angular/common';  ...  export class YourComponent {      constructor(@Inject(DOCUMENT) private document: Document) {          console.log(this.document.location.href);     } } 
like image 144
JayChase Avatar answered Sep 28 '22 05:09

JayChase