Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply css class to a component element when it's created by router-outlet?

I have DOM that looks something like this:

<app>     <router-outlet></router-outlet>     <project>...</project> </app> 

where project element is inserted by the router.

How do I add a class to this element?

like image 660
Kugel Avatar asked Jan 07 '16 00:01

Kugel


People also ask

How do you use a router outlet?

Router-outlet in Angular works as a placeholder which is used to load the different components dynamically based on the activated component or current route state. Navigation can be done using router-outlet directive and the activated component will take place inside the router-outlet to load its content.

Is router outlet a component?

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 router outlet in app component HTML?

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.

Can you have two router outlet in HTML?

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.


2 Answers

Assuming you always want the class applied to this component, you can use host in your component metadata:

@Component({   selector:'project',   host: {       class:'classYouWantApplied'   } }) 

Resulting in:

<app>     <router-outlet></router-outlet>     <project class="classYouWantApplied">...</project> </app> 
like image 85
drew moore Avatar answered Sep 18 '22 20:09

drew moore


use the adjacent sibling selector and the * wildcard to select the element immediately following the router-outlet


styles.css

router-outlet + * {   /* your css */ } 

enter image description here

like image 30
JED Avatar answered Sep 20 '22 20:09

JED