Calvin Hu Calvin Hu - 1 year ago 331
AngularJS Question

Angular2 navigate from a component to a sibling component without tearing down the first

I've just started learning Angular2 and I want to build a very common "list-detail" style page similar to this: . When you click an item in left-bar it initiates a navigation to the center detail view without tearing down the left-bar. What's the best way to achieve this effect?

My current design is like the following:
enter image description here

When I start the home component, the list component(child1) will load immediately and it will call an external API to fetch the names (only names) of a list of items. When clicking a item name on the left, it will create a new Detail-component. The detail component fetch item detail in its ngOnInit method. I've tried using auxiliary routes to nest the two child components on the same page but it does not seem to work. My home routes look like this:

export const HomeRoutes: RouterConfig = [
path: '',
redirectTo: '/home',
pathMatch: 'full'
path: 'home',
component: HomeComponent,
children: [
path: '',
component: ListComponent
path: ':item',
component: DetailComponent
outlet: 'detail'

And in my
I simply have this:



<router-outlet name="detail"></router-outlet>

And the way I navigate to the detail view is calling navigate() in the list-component like this:

onSelect(item : string) {
this.router.navigate(['/detail', item]);

But everytime I click the any of the items the detail view does not show up and it shows error like this:

Error: Uncaught (in promise): Error: Cannot match any routes: 'home/item'

Am I on the right track of using Auxiliary route and is there any better way to achieve this feature?

Answer Source

The current router version has some issues in navigating to aux outlet using routerLink and navigate method. You should build the full URL and use navigateByUrl.


Plunker with example. Click on detail button and then admin. Admin is routed in admin outlet. Open in full screen to see URL formation.

The URL has following semetics

  1. Child path is sperated by /
  2. If a path has siblings then it should be surrounded with parenthesis
  3. Sibling are separated by //
  4. Outlet and path is sperated by :


Anoter so question

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download