This example is focused on how to get current route in angular. This post will give you simple example of angular get current route. we will help you to give example of angular get active route url it's simple example of angular get active route You just need to some step to done angular get active route name.
We can easily get current active route in angular 6, angular 7, angular 8 and angular 9 application.
we will use angular Router library to getting active route in component file. we can easily get active route path with Router.
We might be some time need to get active path or active router url in angular application. so if you need now then i will help you how you can get active url in angular. so you can see basic solution bellow:
You can get active route name like as bellow, i also written full example with output so, you can understand:
this.router.url;
window.location.href;
Now i will give you full example. you can write code on your component file as like bellow:
Component File
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-posts',
templateUrl: './posts.component.html',
styleUrls: ['./posts.component.css']
})
export class PostsComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
console.log(this.router.url);
console.log( window.location.href);
}
}
You can see output also.
Output:
/posts
http://localhost:4200/posts
I hope it can help you...
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example