DEV Community

Cover image for Whats new in angular 15? Dynamic router outlet names
Ilyoskhuja
Ilyoskhuja

Posted on

Whats new in angular 15? Dynamic router outlet names

Angular 15, the latest version of Angular, was released on January 26, 2023, with several new features and improvements. One of the most notable features is the ability to dynamically set router outlet names.

With Angular 15, developers can now assign unique names to each router outlet in an application. This allows for multiple instances of the same component to be rendered, with each instance being controlled by its own set of routes. This makes it easier to organize and manage complex applications, and it opens up new possibilities for building dynamic user interfaces.

Here's how you can set a dynamic router outlet name in Angular 15:

<router-outlet [name]="getOutletName()"></router-outlet>
Enter fullscreen mode Exit fullscreen mode

In the example above, the getOutletName() function returns the desired name for the router outlet. This function can be used to dynamically set the outlet name based on the current state of the application.

For example, consider a component that displays a list of products. With a dynamic router outlet name, you could render multiple instances of this component, each with its own set of products. By assigning a unique name to each router outlet, you can control the flow of data and interactions between the different instances of the component.

Here's an example of how you could define routes for a dynamic router outlet:

const routes: Routes = [
  {
    path: 'products',
    children: [
      {
        outlet: 'list1',
        path: 'list1',
        component: ProductListComponent
      },
      {
        outlet: 'list2',
        path: 'list2',
        component: ProductListComponent
      }
    ]
  }
];

Enter fullscreen mode Exit fullscreen mode

In the example above, the outlet property is used to specify the name of the router outlet for each route. This allows you to render multiple instances of the ProductListComponent and control the flow of data and interactions between them.

In conclusion, dynamic router outlet names is a highly anticipated feature in Angular 15 that makes it easier to manage complex applications and build dynamic user interfaces. With this new feature, Angular developers can now build applications with greater flexibility, control, and organization.

Top comments (0)