How to get data from URL in Angular 8

How to get data from URL in Angular 8

Retrieve Parameters and Query String values from URL in Angular 8: While working with Angular, Many times we need to get data from URL. Data in url can be passed in two ways, first as a parameter and second as a query string. Sometimes we get confuse while fetching data from the url. Let's have a look.

While working with angular, Many times we need to get data from URL. Data in url can be passed in two ways, first as a parameter and second as a query string. Sometimes we get confuse while fetching data from the url. Let's have a look.

1. Accessing Query String Values From URL

We can get the value of query parameters using ActivatedRoute object, queryParamMap. Let's have example below:

Dummy URL : http://localhost:4200/user-list?type=normal&list=active

For accessing the value from above url we can have code like below:

ngOnInit() {
    // first way
    this.userType = this.route.snapshot.queryParamMap.get("userType");
    this.list = this.route.snapshot.queryParamMap.get("list");
    // second way
    this.route.queryParamMap.subscribe(queryParams => {
       this.userType = queryParams.get("userType");
       this.list = queryParams.get("list");
    })
}

2. Accessing the URL Parameters

Angular comes us with the ActivatedRoute object. We can access the URL parameter value in same way its done above with little difference. Data in this type can be accessed with two different ways. One is through route.snapshot.paramMap and the other is through route.paramMap.subscribe. The main difference between the two is that the subscription will continue to update as the parameter changes for that specific route.

Dummy URL : http://localhost:4200/user-list/normal/active

Snapshot

ngOnInit() {
  this.userType = this.route.snapshot.paramMap.get("userType")
}

Subscription

ngOnInit() {
    this.route.paramMap.subscribe(params => {
        this.userType = params.get("userType")
    })
}

Note: Using subscribe is best way to get the value from URL because if value changes within same route then value get updated in the variable. When we use snapshot then value does not update if value of userType updates in same route.

Conclusion:

Retrieving values from URL is not a big task with in Angular application, But basic understanding of receiving values in different ways is neccessary for a developer.

Thank You!

angular web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Wondering how to upgrade your skills in the pandemic? Here's a simple way you can do it.

Corona Virus Pandemic has brought the world to a standstill. Countries are on a major lockdown. Schools, colleges, theatres, gym, clubs, and all other public

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.