Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically set router-outlet name

Is it possible to dynamically set the name of a router-outlet in angular 2? I need to create a generic component that contains a router-outlet.

Template example :

<nav class="nav menu">
    <a *ngFor="let navRoute of navigationRoutes" class="nav-link" [class.selected]="navRoute.isActive" (click)="onActivated(navRoute.route)">{{navRoute.header}}</a>
</nav>
<router-outlet name=[[DO SOME BINDING HERE]]></router-outlet>

navigationRoutes & name are component @inputs

like image 616
Steven Muhr Avatar asked Nov 25 '16 14:11

Steven Muhr


People also ask

Can I have multiple router outlet tags in my app?

You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc. Assuming on load you are bootstraping appComponent.

Can you identify the use of router outlet directive?

The Router-Outlet #The Router-Outlet is a directive that's available from the router library where the Router inserts the component that gets matched based on the current browser's URL. You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios.

What is the use of router outlet ></ router outlet?

The router-outlet is a directive that's available from the @angular/router package and is used by the router to mark where in a template, a matched component should be inserted. Thanks to the router outlet, your app will have multiple views/pages and the app template acts like a shell of your application.


1 Answers

This is so frustrating. I really wish we could get a real solution. Here is my horrible hack, which works in this case because I only have three outlets: a, b, and c. My use case is that I have a three panel system with auxiliary urls and child routes. My "solution" would fall flat if I ever had a system where there was an arbitrary number of named outlets, or if the router outlet names could change dynamically.

What can we do to get on angular's case to get this fixed?

@Component({
  selector: 'my-router-outlet',
  template: `
    <router-outlet 
      *ngIf="route.outlet === 'a'"
      name="a"
    ></router-outlet>
    <router-outlet 
      *ngIf="route.outlet === 'b'"
      name="b"
    ></router-outlet>
    <router-outlet 
      *ngIf="route.outlet === 'c'"
      name="c"
    ></router-outlet>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RouterOutletComponent {
  constructor(public route: ActivatedRoute) {}
}
like image 163
bkinsey808 Avatar answered Sep 18 '22 14:09

bkinsey808