Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 set param of current route in routerLink

Although it looks trivial, I didn't find any similar cases Here's my route :

{
    path: 'folder/:id',
    component: FolderComponent,
    children: [
        {
            path: 'edit/:form',
            component: 'EditorComponent'
        }
    ]
}

When I'm on edit/form1 page, I can't find the way to specify my routerLink directive so that it just changes the :form value, without losing the parent :id value. For now, I need to do that :

1 <a [routerLink]="['../../edit, 'form2']">Form2</a> to get two level up. This works. But it's not that elegant...

2 I tried ['edit', 'form2'], it brings me to folder/:id/edit/form1/folder/form2

3 If I do ['/edit', 'form2'], I get /folder/form2

4 I even tried ['', 'form2'], I get /form2

edit :

5 As suggested I tried ['./edit', 'form2'], but it gives me folder/:id/edit/form1/edit/form2

Just to precise, my link is in editor-component.html and the current url is http://myapp.com/folder/:id/edit/form1

Thx for your help

like image 267
DlafCreative Avatar asked Nov 09 '16 10:11

DlafCreative


People also ask

What is difference between routerLink and routerLink?

What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.

What is difference between routerLink and href?

Href is the basic attribute provided by Html to navigate through pages which reloads the page on click. routerLink is the attribute provided by angular to navigate to different components without reloading the page.

Can we pass object in routerLink?

RouterLink for dynamic dataDynamic data or user-defined objects can be passed from Angular version 7.2 using the state object stored in History API. The state value can be provided using the routerLink directive or navigateByURL.


1 Answers

Try using this ./

<a [routerLink]="['./edit, 'form2']">Form2</a>

Explanation from docs

The first segment name can be prepended with /, ./, or ../:

  1. If the first segment begins with /, the router will look up the route from the root of the app.
  2. If the first segment begins with ./, or doesn't begin with a slash, the router will instead look in the children of the current activated route.
  3. And if the first segment begins with ../, the router will go up one level.

I think number #2 above is what you trying to achive, from what you are describing here :

When I'm on edit/form1 page, I can't find the way to specify my routerLink directive so that it just changes the :form value, without losing the parent :id value


EDIT

Edit in response to OP comment here :

Unfortunately, it doesn't work :(. I get folder/:id/edit/form1/edit/form2. Maybe I forgot to precise that my navbar is on editor-component.html and my current url is http://myapp.com/folder/:id/edit/form1

Since the navbar is on editor-component.html, now we should use '../' instead, like below

 <a [routerLink]="['../form2']">Go To Form 2 From Editor {{id}}</a>

Explanation : The link is on the child componenet, so we use ../ to up one level, try to think like this : our current is http://myapp.com/folder/:id/edit/form1 then ../ will make it up one level to http://myapp.com/folder/:id/edit/, so now we just need to appendform2 into it.

Updated Sample code Plunker

like image 182
Michael Avatar answered Oct 03 '22 04:10

Michael