I am working on an app which has a main view, and inside it renders several named child router outlets. The code is as follows:
app.component.html:
<router-outlet />
app.routes.ts:
export const routes: Routes = [
{ path: '', component: SlotsView, children: [
{
path: '',
outlet: 'slotA',
component: RouterOutlet1,
},{
path: '',
outlet: 'slotB',
component: RouterOutlet2,
}
]
},
];
slots.view.html:
This is a view with some child routes
<br>
<router-outlet name="slotA"></router-outlet>
<br>
<router-outlet name="slotB"></router-outlet>
RouterOutlet1:
- html:
I am Router outlet 1
- less:
:host{
background-color: #f88;
}
- ts:
@Component({
templateUrl: './router-outlet-1.router-outlet.html',
styleUrls: ['./router-outlet-1.router-outlet.less'],
encapsulation: ViewEncapsulation.Emulated,
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true
})
// eslint-disable-next-line @angular-eslint/component-class-suffix
export class RouterOutlet1{
}
RouterOutlet2:
- html:
I am Router outlet 2
- less:
:host{
background-color: #88f;
}
- ts:
@Component({
templateUrl: './router-outlet-2.router-outlet.html',
styleUrls: ['./router-outlet-2.router-outlet.less'],
encapsulation: ViewEncapsulation.Emulated,
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true
})
// eslint-disable-next-line @angular-eslint/component-class-suffix
export class RouterOutlet1{
}
Note that the difference between both router outlets is that router outlet 1 should have a reddish color (#f88), and router outlet 2 should have a bluish color (#88f).
This is what actually happens:
In DevTools I see that both host elements have the same attribute (_nghost-ng-c4243086843)

And that's why the same style is applied to both of them.
If I change the encapsulation on both router outlets to ShadowDom, the result is rendered correctly:
The question:
Is this a bug in Angular which should be reported to the developers, or is there a good reason for Router outlet 1's styles to be applied to Router outlet 2?
I already did a bit of googling and haven't found such a bug report.
I tried changing the encapsulation mode.
- With
Noneno styles are applied with the:hostrule as explained here: Is it possible to style using :host with Encapsulation.None in Angular? - With
ShadowDomit works correctly, but I don't want to use it unless it is necessary


UPDATE
after checking the replicated stackblitz from the user, the below change fixes the issue!
Since the same
ng-componentis present for both router-outlets we are getting this issue, This definetly looks like a bug in angular, because each element should have their own encapsulation if they are from different outlets!To solve this issue, we need to use the
host stylingdirectly on the element so that even if the attributes match, the direct application of the styles ensures the colors are applied to the correct elementssnippet!
outlet 1
outlet 2
Stackblitz DemoNote: This output is coming from
ng-componentwhich is not even a proper component, you can just redirect the url to accomodate both the named router outlets and the components will get rendered instead ofng-component!It looks like a bug but I don't think its replicable in the latest version of angular (17)
Here is a stackblitz that shows the issue not happening!
Stackblitz DemoTo Solve this issue, you can add a
host classon the component decorator, this will ensure the class gets applied on the correct host!outlet 2
outlet 1
Stackblitz Demo