Skip to content
the poor coder

get current url angular

Poor Man
Published
Reading time
2 min read
On this page

How to get current URL in Angular#

If you need to get the current URL in your Angular application, there are a few different ways to do it. Here are some options:

Using the Location Service#

You can use the built-in Location service in Angular to get information about the current URL. Here's how:

JavaScript

import { Component } from '@angular/core';
import { Location } from '@angular/common';

@Component({
  selector: 'my-component',
  template: `
    Current URL: {{ currentUrl }}
  `,
})
export class MyComponent {
  currentUrl: string;

  constructor(private location: Location) {
    this.currentUrl = this.location.path();
  }
}

Here, we're injecting the Location service into our component's constructor and using its path() method to get the current URL. We're then binding that value to the currentUrl property of our component, which we can display in our template.

Using the Window object#

You can also get the current URL by accessing the window object directly. Here's how:

JavaScript

import { Component } from '@angular/core';

@Component({
  selector: 'my-component',
  template: `
    Current URL: {{ currentUrl }}
  `,
})
export class MyComponent {
  currentUrl: string;

  constructor() {
    this.currentUrl = window.location.href;
  }
}

Here, we're accessing the location.href property of the window object, which gives us the current URL. We're then binding that value to the currentUrl property of our component, which we can display in our template.

Using the Router Service#

If you're using Angular's built-in router, you can also get information about the current URL using the Router service. Here's how:

JavaScript

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'my-component',
  template: `
    Current URL: {{ currentUrl }}
  `,
})
export class MyComponent {
  currentUrl: string;

  constructor(private router: Router) {
    this.currentUrl = this.router.url;
  }
}

Here, we're injecting the Router service into our component's constructor and using its url property to get the current URL. We're then binding that value to the currentUrl property of our component, which we can display in our template.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC