How to pass a GET parameter in a URL in Angular 4+ ? Using the route system

Asked

Viewed 4,609 times

2

I need to insert a parameter GET after entering the page with the URL.

For example:

I clicked here and went to page y. http://example/paginay**?= parametroGet**

The get parameter will be an ID, passed by clicking a button that changes the current route.

  • 1

    Take a look at the documentation for query params https://angular.io/guide/router#query-Parameters-and-Fragments

  • 1

    or if the parameter is mandatory https://angular.io/guide/router#route-Definition-with-a-Parameter

  • 1

    @Eduardovargas I managed to do, I put the answer down there, I still didn’t know the right syntax, but you can do what I wanted with queryParams and then use a specific command to get this get in the url, thanks for the help

2 answers

3


Update of found mode:

<a class="icon-plus-details" [routerLink]="['/', linkDetail]" [queryParams]="{id: ownerId}"></a>

That one

[queryParams]="{id: ownerId}"

Inserts into the url returned a parameter get: url_example? id=12 for example.

After that, the shape of the router capture a value get present in a url current. Within the component that is the result of selected route, is done as follows:

Accomplishes all Imports necessary and etc of Routerparam, and then can be returned in a variable via the syntax below present in const id

import { ActivatedRoute, Router } from '@angular/router';

getParam() {
const id = this.routerParam.snapshot.queryParams['id'];
}   

2

On Template

New Exercise

Request:

  
this.route.queryParams
    .filter(params => params.id)
    .subscribe(params => { this.id = params.id;});
  • 1

    In addition to the code, you could explain to the author the proposal of these lines of code!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.