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.
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.
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With