0

My issue is closest to THIS POST. But, it'a slightly a different scenario.

SCENARIO

I have 2 pages main and detail.

Now, in the main page, I have a drop-down the has initially no value selected.

And, in the details page I have a BACK link that points back to main page

CODE

ROUTES

{
  path: 'main',
  component: SomeComponent
},
{
  path: 'main/:dropdown_value',
  component: SomeComponent
}

DETAIL TEMPLATE

<a [routerLink]="['/main', selectedDropDownValue]">BACK</a>

The selectedDropDownValue comes from component and contains the dropdown's selected value from the main page.

PROBLEM

The above implementation works like charm. So, when I hit back the main page, based on the params I passed, I re-select the last selected dropdown value. But, now the main URL becomes:

http://localhost:3000/main/abc

Here, abc is the selected dropdown value

But, I want the URL to stay like

http://localhost:3000/main

It shouldn't show /abc at the end

Abhi
  • 4,123
  • 6
  • 45
  • 77

1 Answers1

0

when we don't want to show data in the URL path than we have to send data via routing using data property of the @RouteConfig annotation provided by angualr2. by using this property we can send additional data to the components at the time of the route configuration without showing it in the URL. here is example of this property.

You can pass the data like this:

[routerLink]="['/ProductDetail', {id:1234}]

The configuration route:

{path: 'product/:id', component: ProductDetailComponentParam ,
                      data: [{isProd: true}]}

Get the data:

constructor(params: RouteParams, data: RouteData){
        this.productID = params.get('id');

        console.log("Is this prod environment?" + data.get('isProd'));
    }

More info on this article:

https://yakovfain.com/2015/11/11/angular-2-passing-data-to-routes/

  • `[routerLink]="['/main', {dropdown_value:'abc'}]`, now shows the URL like **http://192.168.3.4:3000/main;dropdown_value=abc**, which is not what I want. – Abhi Nov 29 '17 at 12:15
  • Yes sorry, reading the docs, i can see the data on router is reintroduced on angular 4.0.0 version. In angular 2 you cant pass data this way. Best option to achieve is a shared service. – Daniel Segura Pérez Nov 29 '17 at 12:20