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
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.
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.
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.
Try using this ./
<a [routerLink]="['./edit, 'form2']">Form2</a>
Explanation from docs
The first segment name can be prepended with /, ./, or ../:
- If the first segment begins with /, the router will look up the route from the root of the app.
- 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.
- 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 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 oneditor-component.html
and my current url ishttp://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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With